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)
178 return 0 if $n < 0 || $n > @checklist;
179 my $ref = $checklist[$n];
180 return 0 unless ref $ref;
183 for ($i = 1; $i < @$ref; $i++) {
184 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
185 return 0 unless $act;
186 next if $blank eq 'b' && $_[$i] =~ /^[ \*]$/;
187 next if $blank eq '*' && $_[$i] =~ /^\*$/;
189 return $i unless is_callsign($_[$i]);
190 } elsif ($act eq 'i') {
192 } elsif ($act eq 'm') {
193 return $i unless is_pctext($_[$i]);
194 } elsif ($act eq 'p') {
195 return $i unless is_pcflag($_[$i]);
196 } elsif ($act eq 'f') {
197 return $i unless is_freq($_[$i]);
198 } elsif ($act eq 'n') {
199 return $i unless $_[$i] =~ /^[\d ]+$/;
200 } elsif ($act eq 'h') {
201 return $i unless $_[$i] =~ /^H\d\d?$/;
202 } elsif ($act eq 'd') {
203 return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
204 } elsif ($act eq 't') {
205 return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
206 } elsif ($act eq 'l') {
207 return $i unless $_[$i] =~ /^[A-Z]$/;
208 } elsif ($act eq 'a') {
209 return $i unless is_ipaddr($_[$i]);
218 my $period = shift || ($self->{do_pc9x} ? $pc92_update_period : $pc92_extnode_update_period);
219 $self->{next_pc92_update} = $main::systime + $period - int rand($period / 4);
220 dbg("ROUTE: update_pc92_next: $self->{call} " . atime($self->{next_pc92_update})) if isdbg('obscount');
223 sub update_pc92_keepalive
226 my $period = shift || $pc92_keepalive_period;
227 $self->{next_pc92_keepalive} = $main::systime + $period - int rand($period / 4);
228 dbg("ROUTE: update_pc92_keepalive: $self->{call} " . atime($self->{next_pc92_keepalive})) if isdbg('obscount');
233 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
236 my $user = DXUser::get($main::mycall);
237 die "User $main::mycall not setup or disappeared RTFM" unless $user;
239 $myprot_version += $main::version*100;
240 $main::me = DXProt->new($main::mycall, 0, $user);
241 $main::me->{here} = 1;
242 $main::me->{state} = "indifferent";
243 $main::me->{sort} = 'S'; # S for spider
244 $main::me->{priv} = 9;
245 $main::me->{metric} = 0;
246 $main::me->{pingave} = 0;
247 $main::me->{registered} = 1;
248 $main::me->{version} = $main::version;
249 $main::me->{build} = "$main::subversion.$main::build";
250 $main::me->{do_pc9x} = 1;
251 $main::me->update_pc92_next($pc92_short_update_period);
252 $main::me->update_pc92_keepalive;
256 # obtain a new connection this is derived from dxchannel
261 my $self = DXChannel::alloc(@_);
263 # add this node to the table, the values get filled in later
267 # if we have an entry already, then send a PC21 to all connect
268 # old style connections, because we are about to get the real deal
269 if (my $ref = Route::Node::get($call)) {
270 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
271 my @rout = $ref->delete;
272 $self->route_pc21($main::mycall, undef, @rout) if @rout;
274 $main::routeroot->add($call, '5000', Route::here(1), $self->{conn}->peerhost) if $call ne $main::mycall;
279 # this is how a pc connection starts (for an incoming connection)
280 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
281 # all the crap that comes between).
284 my ($self, $line, $sort) = @_;
285 my $call = $self->{call};
286 my $user = $self->{user};
289 my $host = $self->{conn}->peerhost;
290 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
293 Log('DXProt', "$call connected from $host");
295 # remember type of connection
296 $self->{consort} = $line;
297 $self->{outbound} = $sort eq 'O';
298 my $priv = $user->priv;
299 $priv = $user->priv(1) unless defined $priv;
300 $self->{priv} = $priv; # other clusters can always be 'normal' users
301 $self->{lang} = $user->lang || 'en';
302 $self->{isolate} = $user->{isolate};
303 $self->{consort} = $line; # save the connection type
307 # sort out registration
308 $self->{registered} = 1;
310 # get the output filters
311 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
312 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
313 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
314 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
315 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate};
316 $self->{pc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
319 # get the INPUT filters (these only pertain to Clusters)
320 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
321 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
322 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
323 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
324 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
325 $self->{inpc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
328 # set unbuffered and no echo
329 $self->send_now('B',"0");
330 $self->send_now('E',"0");
331 $self->conn->echo(0) if $self->conn->can('echo');
333 # ping neighbour node stuff
334 my $ping = $user->pingint;
335 $ping = $pingint unless defined $ping;
336 $self->{pingint} = $ping;
337 $self->{nopings} = $user->nopings || $obscount;
338 $self->{pingtime} = [ ];
339 $self->{pingave} = 999;
340 $self->{metric} ||= 100;
341 $self->{lastping} = $main::systime;
343 # send initialisation string
344 unless ($self->{outbound}) {
348 $self->state('init');
349 $self->{pc50_t} = $main::systime;
351 # send info to all logged in thingies
352 $self->tell_login('loginn');
354 # run a script send the output to the debug file
355 my $script = new Script(lc $call) || new Script('node_default');
356 $script->run($self) if $script;
358 # set up a config broadcast "quite soon" to converge tables quicker
359 $main::me->update_pc92_next($pc92_short_update_period);
360 $self->update_pc92_next($pc92_short_update_period);
362 # set next keepalive time
363 $self->update_pc92_keepalive;
367 # send outgoing 'challenge'
373 $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
377 # This is the normal pcxx despatcher
381 my ($self, $line) = @_;
383 if ($line =~ '^<\w+\s' && $main::do_xml) {
384 DXXml::normal($self, $line);
388 my @field = split /\^/, $line;
389 return unless @field;
391 pop @field if $field[-1] eq '~';
393 # print join(',', @field), "\n";
396 # process PC frames, this will fail unless the frame starts PCnn
397 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
398 unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
399 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
403 # check for and dump bad protocol messages
404 my $n = check($pcno, @field);
406 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
410 # modify the hop count here
411 if ($self != $main::me) {
412 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
416 $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
417 $field[-1] = "H$hops";
421 if (defined &Local::pcprot) {
423 eval { $r = Local::pcprot($self, $pcno, $line, @field); };
424 return if $r; # i.e don't process it
427 # send it out for processing
428 my $origin = $self->{call};
430 my $sub = "handle_$pcno";
432 if ($self->can($sub)) {
433 $self->$sub($pcno, $line, $origin, @field);
435 $self->handle_default($pcno, $line, $origin, @field);
440 # This is called from inside the main cluster processing loop and is used
441 # for despatching commands that are doing some long processing job
446 my @dxchan = DXChannel::get_all();
451 if ($t - $last10 >= 10) {
459 # send out a pc50 on EVERY channel all at once
460 if ($t >= $last_pc50 + $pc50_interval) {
461 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
466 foreach $dxchan (@dxchan) {
467 next unless $dxchan->is_node;
468 next if $dxchan == $main::me;
471 $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
473 # send a ping out on this channel
474 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
475 if ($dxchan->{nopings} <= 0) {
476 dbg("ROUTE: $dxchan->{call} disconnected on ping obscount") if isdbg('obscount');
479 DXXml::Ping::add($main::me, $dxchan->call);
480 $dxchan->{nopings} -= 1;
481 $dxchan->{lastping} = $t;
482 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
483 dbg("ROUTE: $dxchan->{call} ping obscount = $dxchan->{nopings}") if isdbg('obscount');
490 # send out config broadcasts
491 foreach $dxchan (@dxchan) {
492 next unless $dxchan->is_node;
494 # send out a PC92 config record if required for me and
495 # all my non pc9x dependent nodes. But for dependent nodes we only do
496 # this if we have not seen any from anyone else for at least half
497 # of one update period. This should stop quite a bit of excess C
498 # records. Someone will win, it does not really matter who, because
499 # we always believe "us".
500 if ($main::systime >= $dxchan->{next_pc92_update}) {
501 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
502 dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
503 my $ref = Route::Node::get($dxchan->{call});
504 if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_extnode_update_period/2)) {
505 $dxchan->broadcast_pc92_update($dxchan->{call});
507 $dxchan->update_pc92_next;
510 $dxchan->update_pc92_next; # this won't actually do anything, it's just to be tidy
515 # do the keepalive for me, if required
516 if ($main::systime >= $main::me->{next_pc92_keepalive}) {
517 time_out_pc92_routes();
518 $main::me->broadcast_pc92_keepalive($main::mycall);
521 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
522 my ($add, $del) = gen_pc92_changes();
523 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
524 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
525 clear_pc92_changes();
529 if ($main::systime - 3600 > $last_hour) {
530 $last_hour = $main::systime;
535 # finish up a pc context
539 # some active measures
547 my @dxchan = DXChannel::get_all();
551 # send it if it isn't the except list and isn't isolated and still has a hop count
552 # taking into account filtering and so on
553 foreach $dxchan (@dxchan) {
554 next if $dxchan == $main::me;
555 next if $dxchan == $self && $self->is_node;
556 if ($line =~ /PC61/ && !($dxchan->is_spider || $dxchan->is_user)) {
558 my @f = split /\^/, $line;
559 $pc11 = join '^', 'PC11', @f[1..7,9];
561 $dxchan->dx_spot($pc11, $self->{isolate}, @_, $self->{call});
563 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
575 if ($self->{spotsfilter}) {
576 ($filter, $hops) = $self->{spotsfilter}->it(@_);
577 return unless $filter;
579 send_prot_line($self, $filter, $hops, $isolate, $line);
584 my ($self, $filter, $hops, $isolate, $line) = @_;
590 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
592 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
593 return unless $routeit;
596 $self->send($routeit);
598 $self->send($routeit) unless $self->{isolate} || $isolate;
607 my @dxchan = DXChannel::get_all();
609 my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
611 # send it if it isn't the except list and isn't isolated and still has a hop count
612 # taking into account filtering and so on
613 foreach $dxchan (@dxchan) {
614 next if $dxchan == $main::me;
615 next if $dxchan == $self && $self->is_node;
619 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
630 if ($self->{wwvfilter}) {
631 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
632 return unless $filter;
634 send_prot_line($self, $filter, $hops, $isolate, $line)
641 my @dxchan = DXChannel::get_all();
643 my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
645 # send it if it isn't the except list and isn't isolated and still has a hop count
646 # taking into account filtering and so on
647 foreach $dxchan (@dxchan) {
648 next if $dxchan == $main::me;
649 next if $dxchan == $self;
651 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
662 if ($self->{wcyfilter}) {
663 ($filter, $hops) = $self->{wcyfilter}->it(@_);
664 return unless $filter;
666 send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
673 my $from_pc9x = shift;
675 my @dxchan = DXChannel::get_all();
679 my $text = unpad($_[2]);
682 if ($_[3] eq '*') { # sysops
684 } elsif ($_[3] gt ' ') { # speciality list handling
685 my ($name) = split /\./, $_[3];
686 $target = "$name"; # put the rest in later (if bothered)
693 $target = "ALL" if !$target;
696 # obtain country codes etc
697 my @a = Prefix::cty_data($from);
698 my @b = Prefix::cty_data($_[4]);
699 if ($self->{inannfilter}) {
700 my ($filter, $hops) =
701 $self->{inannfilter}->it(@_, $self->{call},
703 @b[0..2], $a[3], $b[3]);
705 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
710 # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
711 if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
712 my $dxchan = DXChannel::get($from);
713 if ($self == $main::me && $dxchan && $dxchan->is_user) {
714 if ($dxchan->priv < 5) {
715 $dxchan->send($dxchan->msg('dup'));
719 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
724 Log('ann', $target, $from, $text);
726 # send it if it isn't the except list and isn't isolated and still has a hop count
727 # taking into account filtering and so on
728 foreach $dxchan (@dxchan) {
729 next if $dxchan == $main::me;
730 next if $dxchan == $self && $self->is_node;
731 next if $from_pc9x && $dxchan->{do_pc9x};
732 next if $target eq 'LOCAL' && $dxchan->is_node;
733 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
738 my $msgid = int rand(1000);
743 $msgid = 1 if $msgid > 999;
751 my $from_pc9x = shift;
753 my @dxchan = DXChannel::get_all();
756 my $text = unpad($_[2]);
760 # munge the group and recast the line if required
761 if ($target =~ s/\.LST$//) {
765 # obtain country codes etc
766 my @a = Prefix::cty_data($from);
767 my @b = Prefix::cty_data($_[4]);
768 if ($self->{inannfilter}) {
769 my ($filter, $hops) =
770 $self->{inannfilter}->it(@_, $self->{call},
772 @b[0..2], $a[3], $b[3]);
774 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
779 if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
780 my $dxchan = DXChannel::get($from);
781 if ($self == $main::me && $dxchan && $dxchan->is_user) {
782 if ($dxchan->priv < 5) {
783 $dxchan->send($dxchan->msg('dup'));
787 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
793 Log('chat', $target, $from, $text);
795 # send it if it isn't the except list and isn't isolated and still has a hop count
796 # taking into account filtering and so on
797 foreach $dxchan (@dxchan) {
798 if ($dxchan->is_node) {
799 next if $dxchan == $main::me;
800 next if $dxchan == $self;
801 next if $from_pc9x && $dxchan->do_pc9x;
802 next unless $dxchan->is_spider && $dxchan->do_pc9x;
803 next if $target eq 'LOCAL';
806 $dxchan->chat($line, $self->{isolate}, $target, $_[1],
807 $text, @_, $self->{call}, @a[0..2], @b[0..2]);
821 if ($self->{annfilter}) {
822 ($filter, $hops) = $self->{annfilter}->it(@_);
823 return unless $filter;
825 send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
838 if ($self->{annfilter}) {
839 ($filter, $hops) = $self->{annfilter}->it(@_);
840 return unless $filter;
842 if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
843 send_prot_line($self, $filter, $hops, $isolate, $line);
848 sub send_local_config
852 dbg('DXProt::send_local_config') if isdbg('trace');
860 if ($self->{isolate}) {
861 dbg("send_local_config: isolated");
862 @localnodes = ( $main::routeroot );
863 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
864 } elsif ($self->{do_pc9x}) {
865 dbg("send_local_config: doing pc9x");
866 my $node = Route::Node::get($self->{call});
867 # $self->send_last_pc92_config($main::routeroot);
868 # $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
869 $self->send(pc92a($main::routeroot, $node));
870 $self->send(pc92k($main::routeroot));
872 # create a list of all the nodes that are not connected to this connection
873 # and are not themselves isolated, this to make sure that isolated nodes
874 # don't appear outside of this node
876 dbg("send_local_config: traditional");
878 # send locally connected nodes
879 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
880 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
881 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
884 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
886 foreach $node (@rawintcalls) {
887 push @intcalls, $node if grep $_ && $node != $_, @intcalls;
889 my $ref = Route::Node::get($self->{call});
890 my @rnodes = $ref->nodes;
891 foreach $node (@intcalls) {
892 push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
894 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
897 # get all the users connected on the above nodes and send them out
898 unless ($self->{do_pc9x}) {
899 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
901 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
902 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
904 dbg("sent a null value") if isdbg('chanerr');
910 sub gen_my_pc92_config
914 if ($node->{call} eq $main::mycall) {
915 clear_pc92_changes(); # remove any slugged data, we are generating it as now
916 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
917 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
918 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
919 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
920 return pc92c($node, @localnodes);
922 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
923 return pc92c($node, @rout);
927 sub send_last_pc92_config
931 if (my $l = $node->last_PC92C) {
934 $self->send_pc92_config($node);
943 dbg('DXProt::send_pc92_config') if isdbg('trace');
945 $node->last_PC92C(gen_my_pc92_config($node));
946 $self->send($node->last_PC92C);
949 sub broadcast_pc92_update
954 dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
956 my $nref = Route::Node::get($call);
958 cluck("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
959 $self->update_pc92_next;
962 my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
963 $nref->lastid(last_pc9x_id());
964 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
965 $self->update_pc92_next;
968 sub broadcast_pc92_keepalive
973 dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
975 my $nref = Route::Node::get($call);
977 cluck("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
978 $self->update_pc92_keepalive;
981 my $l = pc92k($nref);
982 $nref->lastid(last_pc9x_id());
983 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
984 $self->update_pc92_keepalive;
987 sub time_out_pc92_routes
989 my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
991 foreach my $n (@nodes) {
994 if (my $dxchan = DXChannel::get($n->call)) {
995 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
999 my @parents = map {Route::Node::get($_)} $n->parents;
1002 dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('obscount');
1003 push @rdel, $n->del($_);
1007 dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
1011 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1016 # route a message down an appropriate interface for a callsign
1018 # is called route(to, pcline);
1023 my ($self, $call, $line) = @_;
1025 if (ref $self && $call eq $self->{call}) {
1026 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1030 # always send it down the local interface if available
1031 my $dxchan = DXChannel::get($call);
1033 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1035 my $cl = Route::get($call);
1036 $dxchan = $cl->dxchan if $cl;
1038 if (ref $self && $dxchan eq $self) {
1039 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1042 dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1047 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1049 $dxchan->send($routeit) unless $dxchan == $main::me;
1052 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1057 # obtain the hops from the list for this callsign and pc no
1063 my $hops = $DXProt::hopcount{$pcno};
1064 $hops = $DXProt::def_hopcount if !$hops;
1069 # adjust the hop count on a per node basis using the user loadable
1070 # hop table if available or else decrement an existing one
1077 my $call = $self->{call};
1080 if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1081 my ($pcno) = $s =~ /^PC(\d\d)/o;
1082 confess "$call called adjust_hops with '$s'" unless $pcno;
1083 my $ref = $nodehops{$call} if %nodehops;
1085 my $newhops = $ref->{$pcno};
1086 return "" if defined $newhops && $newhops == 0;
1087 $newhops = $ref->{default} unless $newhops;
1088 return "" if defined $newhops && $newhops == 0;
1089 $newhops = $hops unless $newhops;
1090 return "" unless $newhops > 0;
1091 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1093 return "" unless $hops > 0;
1105 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1106 do "$main::data/hop_table.pl";
1113 my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1114 if ($tonode eq $main::mycall) {
1115 my $ref = DXUser::get_current($fromnode);
1116 unless ($ref && UNIVERSAL::isa($ref, 'DXUser')) {
1117 dbg("DXProt process_rcmd: user $fromnode isn't a reference (check user_asc and tell G1TLH)");
1118 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1121 my $cref = Route::Node::get($fromnode);
1122 unless ($cref && UNIVERSAL::isa($cref, 'Route')) {
1123 dbg("DXProt process_rcmd: Route $fromnode isn't a reference (tell G1TLH)");
1124 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1127 Log('rcmd', 'in', ($ref->{priv}||0), $fromnode, $cmd);
1128 if ($cmd !~ /^\s*rcmd/i && $ref->homenode && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1129 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1130 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1131 my $oldpriv = $self->{priv};
1132 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1133 my @in = (DXCommandmode::run_cmd($self, $cmd));
1134 $self->{priv} = $oldpriv;
1135 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1136 delete $self->{remotecmd};
1138 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1141 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1144 my $ref = DXUser::get_current($tonode);
1145 if ($ref && $ref->is_clx) {
1146 $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1148 $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1153 sub process_rcmd_reply
1155 my ($self, $tonode, $fromnode, $user, $line) = @_;
1156 if ($tonode eq $main::mycall) {
1157 my $s = $rcmds{$fromnode};
1159 my $dxchan = DXChannel::get($s->{call});
1160 my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1161 $ref->send($line) if $ref;
1162 delete $rcmds{$fromnode} if !$dxchan;
1164 # send unsolicited ones to the sysop
1165 my $dxchan = DXChannel::get($main::myalias);
1166 $dxchan->send($line) if $dxchan;
1169 my $ref = DXUser::get_current($tonode);
1170 if ($ref && $ref->is_clx) {
1171 $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1173 $self->route($tonode, pc35($fromnode, $tonode, $line));
1182 my $fromnode = shift;
1187 Log('rcmd', 'out', $fromnode, $line);
1188 if ($self->is_clx) {
1189 $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1191 $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1196 # add a rcmd request to the rcmd queues
1199 my ($self, $to, $cmd) = @_;
1202 $r->{call} = $self->{call};
1203 $r->{t} = $main::systime;
1207 my $ref = Route::Node::get($to);
1208 my $dxchan = $ref->dxchan;
1209 if ($dxchan && $dxchan->is_clx) {
1210 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1212 route(undef, $to, pc34($main::mycall, $to, $cmd));
1219 my $pc39flag = shift || 0;
1220 my $call = $self->call;
1222 return if $self->{disconnecting}++;
1224 unless ($pc39flag == 1) {
1225 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1228 # get rid of any PC16/17/19
1229 eph_del_regex("^PC1[679]*$call");
1231 # do routing stuff, remove me from routing table
1232 my $node = Route::Node::get($call);
1234 Route::delete_interface($call);
1236 # unbusy and stop and outgoing mail
1237 my $mref = DXMsg::get_busy($call);
1238 $mref->stop_msg($call) if $mref;
1240 # remove outstanding pings
1241 delete $pings{$call};
1243 # I was the last node visited
1244 $self->user->node($main::mycall);
1246 # send info to all logged in thingies
1247 $self->tell_login('logoutn');
1249 Log('DXProt', $call . " Disconnected");
1251 $self->SUPER::disconnect;
1253 # here we determine what needs to go out of the routing table
1255 if ($node && $pc39flag != 2) {
1256 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1258 @rout = $node->del($main::routeroot);
1260 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1262 # now we need to see what can't be routed anymore and came
1263 # in via this node (probably).
1265 while ($n != @rout) {
1267 for (Route::Node::get_all()) {
1268 unless ($_->dxchan) {
1269 push @rout, $_->delete;
1272 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1275 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1277 # and all my ephemera as well
1280 eph_del_regex("^PC1[679].*$c");
1284 # broadcast to all other nodes that all the nodes connected to via me are gone
1285 unless ($pc39flag == 2) {
1286 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1287 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1293 # send a talk message to this thingy
1297 my ($self, $from, $to, $via, $line, $origin) = @_;
1299 if ($self->{do_pc9x}) {
1300 $self->send(pc93($to, $from, $via, $line));
1302 $self->send(pc10($from, $to, $via, $line, $origin));
1304 Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1307 # send it if it isn't the except list and isn't isolated and still has a hop count
1308 # taking into account filtering and so on
1314 my $generate = shift;
1315 my $no = shift; # the no of things to filter on
1317 my ($filter, $hops);
1320 for (; @_ && $no; $no--) {
1323 # don't send messages with $self's call in back to them
1324 if ($r->call eq $self->{call}) {
1325 dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1329 if (!$self->{isolate} && $self->{routefilter}) {
1332 ($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});
1336 dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1339 dbg("was sent a null value") if isdbg('chanerr');
1342 push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1346 foreach my $line (&$generate(@rin, @_)) {
1349 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1351 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1352 next unless $routeit;
1355 $self->send($routeit);
1360 # broadcast everywhere
1365 my $generate = shift;
1367 my @dxchan = DXChannel::get_all_nodes();
1371 $line =~ /\^H(\d+)\^?\~?$/;
1372 return unless $1 > 0;
1374 unless ($self->{isolate}) {
1375 foreach $dxchan (@dxchan) {
1376 next if $dxchan == $self || $dxchan == $main::me;
1377 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1378 next unless $dxchan->isa('DXProt');
1380 $dxchan->send_route($origin, $generate, @_);
1385 # broadcast to non-pc9x nodes
1386 sub broadcast_route_nopc9x
1390 my $generate = shift;
1392 my @dxchan = DXChannel::get_all_nodes();
1396 $line =~ /\^H(\d+)\^?\~?$/;
1397 return unless $1 > 0;
1399 unless ($self->{isolate}) {
1400 foreach $dxchan (@dxchan) {
1401 next if $dxchan == $self || $dxchan == $main::me;
1402 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1403 next unless $dxchan->isa('DXProt');
1404 next if $dxchan->{do_pc9x};
1405 if ($generate == \&pc16 || $generate==\&pc17) {
1406 next unless $dxchan->user->wantsendpc16;
1408 $dxchan->send_route($origin, $generate, @_);
1413 # this is only used for next door nodes on init
1418 return unless $self->{do_pc9x};
1421 my $generate = shift;
1422 my $no = shift; # the no of things to filter on
1425 $line = &$generate(@_);
1429 # broadcast only to pc9x nodes
1430 sub broadcast_route_pc9x
1434 my $generate = shift;
1437 my @dxchan = DXChannel::get_all_nodes();
1440 if ($origin eq $main::mycall && $generate && !$line) {
1441 $line = &$generate(@_);
1444 $line =~ /\^H(\d+)\^\~?$/;
1445 unless ($1 > 0 && $self->{isolate}) {
1446 foreach $dxchan (@dxchan) {
1447 next if $dxchan == $self || $dxchan == $main::me;
1448 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1449 next unless $dxchan->isa('DXProt');
1450 next unless $dxchan->{do_pc9x};
1452 $dxchan->send($line);
1460 return unless $self->user->wantpc16;
1463 broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1469 return unless $self->user->wantpc16;
1472 broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1480 broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1488 broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1496 broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1504 broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1507 # this is probably obsolete now
1514 broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1522 broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1530 broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1538 broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1545 my ($filter, $hops) = (1, 1);
1547 if ($self->{inroutefilter}) {
1548 ($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);
1549 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1557 my $t = shift || $eph_restime;
1561 $s =~ s/\^H\d\d?\^?\~?$//;
1562 $r = 1 if exists $eph{$s}; # pump up the dup if it keeps circulating
1563 $eph{$s} = $main::systime + $t;
1564 dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1572 while (($key, $val) = each %eph) {
1573 if ($key =~ m{$regex}) {
1583 while (($key, $val) = each %eph) {
1584 if ($main::systime >= $val) {
1595 while (($key, $val) = each %eph) {
1596 push @out, $key, $val;
1603 goto &DXCommandmode::run_cmd;
1607 # import any msgs in the chat directory
1608 # the messages are sent to the chat group which forms the
1609 # the first part of the name (eg: solar.1243.txt would be
1610 # sent to chat group SOLAR)
1612 # Each message found is sent: one non-blank line to one chat
1613 # message. So 4 lines = 4 chat messages.
1615 # The special name LOCAL is for local users ANN
1616 # The special name ALL is for ANN/FULL
1617 # The special name SYSOP is for ANN/SYSOP
1621 # are there any to do in this directory?
1622 return unless -d $chatimportfn;
1623 unless (opendir(DIR, $chatimportfn)) {
1624 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1625 Log('msg', "can\'t open $chatimportfn $!");
1629 my @names = readdir(DIR);
1632 foreach $name (@names) {
1633 next if $name =~ /^\./;
1634 my $splitit = $name =~ /^split/;
1635 my $fn = "$chatimportfn/$name";
1637 unless (open(MSG, $fn)) {
1638 dbg("can\'t open import file $fn $!") if isdbg('msg');
1639 Log('msg', "can\'t open import file $fn $!");
1643 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1647 my @cat = split /\./, $name;
1648 my $target = uc $cat[0];
1650 foreach my $text (@msg) {
1651 next unless $text && $text !~ /^\s*#/;
1652 if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1653 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1654 my $wx = uc $target eq 'WX' ? '1' : '0';
1656 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1657 Log('ann', $target, $main::mycall, $text);
1658 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1660 DXCommandmode::send_chats($main::me, $target, $text);
1666 # start a pc92 find operation
1671 my $key = "$dxchan->{call}|$target";
1672 if ($pc92_find{$key}) {
1677 # function (not method) to handle pc92 find returns
1678 sub handle_pc92_find_reply
1680 my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1682 $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');