prevent PC61 going to non-spider clusters
[spider.git] / perl / DXProt.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 #
8 #
9
10 package DXProt;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXM;
18 use DXProtVars;
19 use DXCommandmode;
20 use DXLog;
21 use Spot;
22 use DXProtout;
23 use DXDebug;
24 use Filter;
25 use Local;
26 use DXDb;
27 use AnnTalk;
28 use Geomag;
29 use WCY;
30 use BadWords;
31 use DXHash;
32 use Route;
33 use Route::Node;
34 use Script;
35 use RouteDB;
36 use DXProtHandle;
37
38 use strict;
39
40 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
41                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
42                         $pingint $obscount %pc19list $chatdupeage $chatimportfn
43                         $pc19_version $myprot_version
44                         %nodehops $baddx $badspotter $badnode $censorpc
45                         $allowzero $decode_dk0wcy $send_opernam @checklist
46                         $eph_pc15_restime $pc92_update_period $pc92_obs_timeout
47                         %pc92_find $pc92_find_timeout $pc92_short_update_period
48                         $next_pc92_obs_timeout $pc92_slug_changes $last_pc92_slug
49                         $pc92_extnode_update_period $pc50_interval
50                         $pc92_keepalive_period
51                    );
52
53 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
54 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
55
56 $last_hour = time;                              # last time I did an hourly periodic update
57 %rcmds = ();                    # outstanding rcmd requests outbound
58 %nodehops = ();                 # node specific hop control
59 %pc19list = ();                                 # list of outstanding PC19s that haven't had PC16s on them
60
61 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
62                                                                 # loads of 'bad things'
63 $baddx = new DXHash "baddx";
64 $badspotter = new DXHash "badspotter";
65 $badnode = new DXHash "badnode";
66 $last10 = $last_pc50 = time;
67 $ann_to_talk = 1;
68 $eph_restime = 60;
69 $eph_info_restime = 60*60;
70 $eph_pc15_restime = 6*60;
71 $eph_pc34_restime = 30;
72 $pingint = 5*60;
73 $obscount = 2;
74 $chatdupeage = 20 * 60;
75 $chatimportfn = "$main::root/chat_import";
76 $pc19_version = 5454;                   # the visible version no for outgoing PC19s generated from pc59
77 $pc92_update_period = 4*60*60;  # the period between outgoing PC92 C updates
78 $pc92_short_update_period = 15*60; # shorten the update period after a connection or start up
79 $pc92_extnode_update_period = 1*60*60; # the update period for external nodes
80 $pc92_keepalive_period = 1*60*60;       # frequency of PC92 K (keepalive) records
81 %pc92_find = ();                                # outstanding pc92 find operations
82 $pc92_find_timeout = 30;                # maximum time to wait for a reply
83
84
85 @checklist =
86 (
87  [ qw(i c c m bp bc c) ],                       # pc10
88  [ qw(i f m d t m c c h) ],             # pc11
89  [ qw(i c bm m bm bm p h) ],            # pc12
90  [ qw(i c h) ],                                 #
91  [ qw(i c h) ],                                 #
92  [ qw(i c m h) ],                                       #
93  undef ,                                                # pc16 has to be validated manually
94  [ qw(i c c h) ],                                       # pc17
95  [ qw(i m n) ],                                 # pc18
96  undef ,                                                # pc19 has to be validated manually
97  undef ,                                                # pc20 no validation
98  [ qw(i c m h) ],                                       # pc21
99  undef ,                                                # pc22 no validation
100  [ qw(i d n n n n m c c h) ],           # pc23
101  [ qw(i c p h) ],                                       # pc24
102  [ qw(i c c n n) ],                             # pc25
103  [ qw(i f m d t m c c bc) ],            # pc26
104  [ qw(i d n n n n m c c bc) ],  # pc27
105  [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
106  [ qw(i c c n m) ],                             # pc29
107  [ qw(i c c n) ],                                       # pc30
108  [ qw(i c c n) ],                                       # pc31
109  [ qw(i c c n) ],                                       # pc32
110  [ qw(i c c n) ],                                       # pc33
111  [ qw(i c c m) ],                                       # pc34
112  [ qw(i c c m) ],                                       # pc35
113  [ qw(i c c m) ],                                       # pc36
114  [ qw(i c c n m) ],                             # pc37
115  undef,                                                 # pc38 not interested
116  [ qw(i c m) ],                                 # pc39
117  [ qw(i c c m p n) ],                           # pc40
118  [ qw(i c n m h) ],                             # pc41
119  [ qw(i c c n) ],                                       # pc42
120  undef,                                                 # pc43 don't handle it
121  [ qw(i c c n m m c) ],                 # pc44
122  [ qw(i c c n m) ],                             # pc45
123  [ qw(i c c n) ],                                       # pc46
124  undef,                                                 # pc47
125  undef,                                                 # pc48
126  [ qw(i c m h) ],                                       # pc49
127  [ qw(i c n h) ],                                       # pc50
128  [ qw(i c c n) ],                                       # pc51
129  undef,
130  undef,
131  undef,
132  undef,
133  undef,
134  undef,
135  undef,
136  undef,
137  undef,                                                 # pc60
138  undef,
139  undef,
140  undef,
141  undef,
142  undef,
143  undef,
144  undef,
145  undef,
146  undef,
147  undef,                                                 # pc70
148  undef,
149  undef,
150  [ qw(i d n n n n n n m m m c c h) ],   # pc73
151  undef,
152  undef,
153  undef,
154  undef,
155  undef,
156  undef,
157  undef,                                                 # pc80
158  undef,
159  undef,
160  undef,
161  [ qw(i c c c m) ],                             # pc84
162  [ qw(i c c c m) ],                             # pc85
163  undef,
164  undef,
165  undef,
166  undef,
167  undef,                                                 # pc90
168  undef,
169  [ qw(i c f l)],                                # pc92
170  [ qw(i c f *m c *c m)],                                        # pc93
171 );
172
173 # use the entry in the check list to check the field list presented
174 # return OK if line NOT in check list (for now)
175 sub check
176 {
177         my $n = shift;
178         $n -= 10;
179         return 0 if $n < 0 || $n > @checklist;
180         my $ref = $checklist[$n];
181         return 0 unless ref $ref;
182
183         my $i;
184         for ($i = 1; $i < @$ref; $i++) {
185                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
186                 return 0 unless $act;
187                 next if $blank eq 'b' && $_[$i] =~ /^[ \*]$/;
188                 next if $blank eq '*' && $_[$i] =~ /^\*$/;
189                 if ($act eq 'c') {
190                         return $i unless is_callsign($_[$i]);
191                 } elsif ($act eq 'i') {
192                         ;                                       # do nothing
193                 } elsif ($act eq 'm') {
194                         return $i unless is_pctext($_[$i]);
195                 } elsif ($act eq 'p') {
196                         return $i unless is_pcflag($_[$i]);
197                 } elsif ($act eq 'f') {
198                         return $i unless is_freq($_[$i]);
199                 } elsif ($act eq 'n') {
200                         return $i unless $_[$i] =~ /^[\d ]+$/;
201                 } elsif ($act eq 'h') {
202                         return $i unless $_[$i] =~ /^H\d\d?$/;
203                 } elsif ($act eq 'd') {
204                         return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
205                 } elsif ($act eq 't') {
206                         return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
207                 } elsif ($act eq 'l') {
208                         return $i unless $_[$i] =~ /^[A-Z]$/;
209                 }
210         }
211         return 0;
212 }
213
214 sub update_pc92_next
215 {
216         my $self = shift;
217         my $period = shift || ($self->{do_pc9x} ? $pc92_update_period : $pc92_extnode_update_period);
218         $self->{next_pc92_update} = $main::systime + $period - int rand($period / 4);
219         dbg("ROUTE: update_pc92_next: $self->{call} " . atime($self->{next_pc92_update})) if isdbg('obscount');
220 }
221
222 sub update_pc92_keepalive
223 {
224         my $self = shift;
225         my $period = shift || $pc92_keepalive_period;
226         $self->{next_pc92_keepalive} = $main::systime + $period - int rand($period / 4);
227         dbg("ROUTE: update_pc92_keepalive: $self->{call} " . atime($self->{next_pc92_keepalive})) if isdbg('obscount');
228 }
229
230 sub init
231 {
232         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
233         confess $@ if $@;
234
235         my $user = DXUser->get($main::mycall);
236         die "User $main::mycall not setup or disappeared RTFM" unless $user;
237
238         $myprot_version += $main::version*100;
239         $main::me = DXProt->new($main::mycall, 0, $user);
240         $main::me->{here} = 1;
241         $main::me->{state} = "indifferent";
242         $main::me->{sort} = 'S';    # S for spider
243         $main::me->{priv} = 9;
244         $main::me->{metric} = 0;
245         $main::me->{pingave} = 0;
246         $main::me->{registered} = 1;
247         $main::me->{version} = $main::version;
248         $main::me->{build} = "$main::subversion.$main::build";
249         $main::me->{do_pc9x} = 1;
250         $main::me->update_pc92_next($pc92_short_update_period);
251         $main::me->update_pc92_keepalive;
252 }
253
254 #
255 # obtain a new connection this is derived from dxchannel
256 #
257
258 sub new
259 {
260         my $self = DXChannel::alloc(@_);
261
262         # add this node to the table, the values get filled in later
263         my $pkg = shift;
264         my $call = shift;
265
266         # if we have an entry already, then send a PC21 to all connect
267         # old style connections, because we are about to get the real deal
268         if (my $ref = Route::Node::get($call)) {
269                 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
270                 my @rout = $ref->delete;
271                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
272         }
273         $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
274
275         return $self;
276 }
277
278 # this is how a pc connection starts (for an incoming connection)
279 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
280 # all the crap that comes between).
281 sub start
282 {
283         my ($self, $line, $sort) = @_;
284         my $call = $self->{call};
285         my $user = $self->{user};
286
287         # log it
288         my $host = $self->{conn}->{peerhost};
289         $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
290         $host ||= "unknown";
291
292         Log('DXProt', "$call connected from $host");
293
294         # remember type of connection
295         $self->{consort} = $line;
296         $self->{outbound} = $sort eq 'O';
297         my $priv = $user->priv;
298         $priv = $user->priv(1) unless $priv;
299         $self->{priv} = $priv;     # other clusters can always be 'normal' users
300         $self->{lang} = $user->lang || 'en';
301         $self->{isolate} = $user->{isolate};
302         $self->{consort} = $line;       # save the connection type
303         $self->{here} = 1;
304         $self->{width} = 80;
305
306         # sort out registration
307         $self->{registered} = 1;
308
309         # get the output filters
310         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
311         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
312         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
313         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
314         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
315
316
317         # get the INPUT filters (these only pertain to Clusters)
318         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
319         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
320         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
321         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
322         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
323         # if there is no route input filter then specify a default one.
324         # obviously this can be changed later by the sysop.
325         if (!$self->{inroutefilter}) {
326                 my $dxcc = $self->dxcc;
327                 $Route::filterdef->cmd($self, 'route', 'accept', "input by_dxcc $dxcc" );
328         }
329
330         # set unbuffered and no echo
331         $self->send_now('B',"0");
332         $self->send_now('E',"0");
333         $self->conn->echo(0) if $self->conn->can('echo');
334
335         # ping neighbour node stuff
336         my $ping = $user->pingint;
337         $ping = $pingint unless defined $ping;
338         $self->{pingint} = $ping;
339         $self->{nopings} = $user->nopings || $obscount;
340         $self->{pingtime} = [ ];
341         $self->{pingave} = 999;
342         $self->{metric} ||= 100;
343         $self->{lastping} = $main::systime;
344
345         # send initialisation string
346         unless ($self->{outbound}) {
347                 $self->sendinit;
348         }
349
350         $self->state('init');
351         $self->{pc50_t} = $main::systime;
352
353         # send info to all logged in thingies
354         $self->tell_login('loginn');
355
356         # run a script send the output to the debug file
357         my $script = new Script(lc $call) || new Script('node_default');
358         $script->run($self) if $script;
359
360         # set up a config broadcast "quite soon" to converge tables quicker
361         $main::me->update_pc92_next($pc92_short_update_period);
362         $self->update_pc92_next($pc92_short_update_period);
363
364         # set next keepalive time
365         $self->update_pc92_keepalive;
366 }
367
368 #
369 # send outgoing 'challenge'
370 #
371
372 sub sendinit
373 {
374         my $self = shift;
375         $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
376 }
377
378 #
379 # This is the normal pcxx despatcher
380 #
381 sub normal
382 {
383         my ($self, $line) = @_;
384
385         if ($line =~ '^<\w+\s' && $main::do_xml) {
386                 DXXml::normal($self, $line);
387                 return;
388         }
389
390         my @field = split /\^/, $line;
391         return unless @field;
392
393         pop @field if $field[-1] eq '~';
394
395 #       print join(',', @field), "\n";
396
397
398         # process PC frames, this will fail unless the frame starts PCnn
399         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
400         unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
401                 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
402                 return;
403         }
404
405         # check for and dump bad protocol messages
406         my $n = check($pcno, @field);
407         if ($n) {
408                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
409                 return;
410         }
411
412         # modify the hop count here
413         if ($self != $main::me) {
414                 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
415                         $trail ||= '';
416                         $hops--;
417                         return if $hops < 0;
418                         $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
419                         $field[-1] = "H$hops";
420                 }
421         }
422
423         # send it out for processing
424         my $origin = $self->{call};
425         no strict 'subs';
426         my $sub = "handle_$pcno";
427
428         if ($self->can($sub)) {
429                 $self->$sub($pcno, $line, $origin, @field);
430         } else {
431                 $self->handle_default($pcno, $line, $origin, @field);
432         }
433 }
434
435 #
436 # This is called from inside the main cluster processing loop and is used
437 # for despatching commands that are doing some long processing job
438 #
439 sub process
440 {
441         my $t = time;
442         my @dxchan = DXChannel::get_all();
443         my $dxchan;
444         my $pc50s;
445
446         # every ten seconds
447         if ($t - $last10 >= 10) {
448                 # clean out ephemera
449
450                 eph_clean();
451                 import_chat();
452
453                 $last10 = $t;
454
455                 # send out a pc50 on EVERY channel all at once
456                 if ($t >= $last_pc50 + $pc50_interval) {
457                         $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
458                         eph_dup($pc50s);
459                         $last_pc50 = $t;
460                 }
461
462                 foreach $dxchan (@dxchan) {
463                         next unless $dxchan->is_node;
464                         next if $dxchan == $main::me;
465
466                         # send the pc50
467                         $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
468
469                         # send a ping out on this channel
470                         if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
471                                 if ($dxchan->{nopings} <= 0) {
472                                         dbg("ROUTE: $dxchan->{call} disconnected on ping obscount") if isdbg('obscount');
473                                         $dxchan->disconnect;
474                                 } else {
475                                         DXXml::Ping::add($main::me, $dxchan->call);
476                                         $dxchan->{nopings} -= 1;
477                                         $dxchan->{lastping} = $t;
478                                         $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
479                                         dbg("ROUTE: $dxchan->{call} ping obscount = $dxchan->{nopings}") if isdbg('obscount');
480                                 }
481                         }
482                 }
483
484                 clean_pc92_find();
485
486                 # send out config broadcasts
487                 foreach $dxchan (@dxchan) {
488                         next unless $dxchan->is_node;
489
490                         # send out a PC92 config record if required for me and
491                         # all my non pc9x dependent nodes. But for dependent nodes we only do
492                         # this if we have not seen any from anyone else for at least half
493                         # of one update period. This should stop quite a bit of excess C
494                         # records. Someone will win, it does not really matter who, because
495                         # we always believe "us".
496                         if ($main::systime >= $dxchan->{next_pc92_update}) {
497                                 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
498                                         dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
499                                         my $ref = Route::Node::get($dxchan->{call});
500                                         if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_extnode_update_period/2)) {
501                                                 $dxchan->broadcast_pc92_update($dxchan->{call});
502                                         } else {
503                                                 $dxchan->update_pc92_next;
504                                         }
505                                 } else {
506                                         $dxchan->update_pc92_next; # this won't actually do anything, it's just to be tidy
507                                 }
508                         }
509                 }
510
511                 # do the keepalive for me, if required
512                 if ($main::systime >= $main::me->{next_pc92_keepalive}) {
513                         time_out_pc92_routes();
514                         $main::me->broadcast_pc92_keepalive($main::mycall);
515                 }
516
517                 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
518                         my ($add, $del) = gen_pc92_changes();
519                         $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
520                         $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
521                         clear_pc92_changes();
522                 }
523         }
524
525         if ($main::systime - 3600 > $last_hour) {
526                 $last_hour = $main::systime;
527         }
528 }
529
530 #
531 # finish up a pc context
532 #
533
534 #
535 # some active measures
536 #
537
538
539 sub send_dx_spot
540 {
541         my $self = shift;
542         my $line = shift;
543         my @dxchan = DXChannel::get_all();
544         my $dxchan;
545
546         # send it if it isn't the except list and isn't isolated and still has a hop count
547         # taking into account filtering and so on
548         foreach $dxchan (@dxchan) {
549                 next if $dxchan == $main::me;
550                 next if $dxchan == $self && $self->is_node;
551                 next if $line =~ /PC61/ && !$dxchan->is_spider && !$dxchan->is_user;
552                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
553         }
554 }
555
556 sub dx_spot
557 {
558         my $self = shift;
559         my $line = shift;
560         my $isolate = shift;
561         my ($filter, $hops);
562
563         if ($self->{spotsfilter}) {
564                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
565                 return unless $filter;
566         }
567         send_prot_line($self, $filter, $hops, $isolate, $line);
568 }
569
570 sub send_prot_line
571 {
572         my ($self, $filter, $hops, $isolate, $line) = @_;
573         my $routeit;
574
575
576         if ($hops) {
577                 $routeit = $line;
578                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
579         } else {
580                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
581                 return unless $routeit;
582         }
583         if ($filter) {
584                 $self->send($routeit);
585         } else {
586                 $self->send($routeit) unless $self->{isolate} || $isolate;
587         }
588 }
589
590
591 sub send_wwv_spot
592 {
593         my $self = shift;
594         my $line = shift;
595         my @dxchan = DXChannel::get_all();
596         my $dxchan;
597         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
598
599         # send it if it isn't the except list and isn't isolated and still has a hop count
600         # taking into account filtering and so on
601         foreach $dxchan (@dxchan) {
602                 next if $dxchan == $main::me;
603                 next if $dxchan == $self && $self->is_node;
604                 my $routeit;
605                 my ($filter, $hops);
606
607                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
608         }
609 }
610
611 sub wwv
612 {
613         my $self = shift;
614         my $line = shift;
615         my $isolate = shift;
616         my ($filter, $hops);
617
618         if ($self->{wwvfilter}) {
619                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
620                 return unless $filter;
621         }
622         send_prot_line($self, $filter, $hops, $isolate, $line)
623 }
624
625 sub send_wcy_spot
626 {
627         my $self = shift;
628         my $line = shift;
629         my @dxchan = DXChannel::get_all();
630         my $dxchan;
631         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
632
633         # send it if it isn't the except list and isn't isolated and still has a hop count
634         # taking into account filtering and so on
635         foreach $dxchan (@dxchan) {
636                 next if $dxchan == $main::me;
637                 next if $dxchan == $self;
638
639                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
640         }
641 }
642
643 sub wcy
644 {
645         my $self = shift;
646         my $line = shift;
647         my $isolate = shift;
648         my ($filter, $hops);
649
650         if ($self->{wcyfilter}) {
651                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
652                 return unless $filter;
653         }
654         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
655 }
656
657 # send an announce
658 sub send_announce
659 {
660         my $self = shift;
661         my $from_pc9x = shift;
662         my $line = shift;
663         my @dxchan = DXChannel::get_all();
664         my $dxchan;
665         my $target = $_[6];
666         my $to = 'To ';
667         my $text = unpad($_[2]);
668         my $from = $_[0];
669
670         if ($_[3] eq '*') {     # sysops
671                 $target = "SYSOP";
672         } elsif ($_[3] gt ' ') { # speciality list handling
673                 my ($name) = split /\./, $_[3];
674                 $target = "$name"; # put the rest in later (if bothered)
675         }
676
677         if ($_[5] eq '1') {
678                 $target = "WX";
679                 $to = '';
680         }
681         $target = "ALL" if !$target;
682
683
684         # obtain country codes etc
685         my @a = Prefix::cty_data($from);
686         my @b = Prefix::cty_data($_[4]);
687         if ($self->{inannfilter}) {
688                 my ($filter, $hops) =
689                         $self->{inannfilter}->it(@_, $self->{call},
690                                                                          @a[0..2],
691                                                                          @b[0..2], $a[3], $b[3]);
692                 unless ($filter) {
693                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
694                         return;
695                 }
696         }
697
698         # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
699         if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
700                 my $dxchan = DXChannel::get($from);
701                 if ($self == $main::me && $dxchan && $dxchan->is_user) {
702                         if ($dxchan->priv < 5) {
703                                 $dxchan->send($dxchan->msg('dup'));
704                                 return;
705                         }
706                 } else {
707                         dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
708                         return;
709                 }
710         }
711
712         Log('ann', $target, $from, $text);
713
714         # send it if it isn't the except list and isn't isolated and still has a hop count
715         # taking into account filtering and so on
716         foreach $dxchan (@dxchan) {
717                 next if $dxchan == $main::me;
718                 next if $dxchan == $self && $self->is_node;
719                 next if $from_pc9x && $dxchan->{do_pc9x};
720                 next if $target eq 'LOCAL' && $dxchan->is_node;
721                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
722                                                   @a[0..2], @b[0..2]);
723         }
724 }
725
726 my $msgid = int rand(1000);
727
728 sub nextchatmsgid
729 {
730         $msgid++;
731         $msgid = 1 if $msgid > 999;
732         return $msgid;
733 }
734
735 # send a chat line
736 sub send_chat
737 {
738         my $self = shift;
739         my $from_pc9x = shift;
740         my $line = shift;
741         my @dxchan = DXChannel::get_all();
742         my $dxchan;
743         my $target = $_[3];
744         my $text = unpad($_[2]);
745         my $ak1a_line;
746         my $from = $_[0];
747
748         # munge the group and recast the line if required
749         if ($target =~ s/\.LST$//) {
750                 $ak1a_line = $line;
751         }
752
753         # obtain country codes etc
754         my @a = Prefix::cty_data($from);
755         my @b = Prefix::cty_data($_[4]);
756         if ($self->{inannfilter}) {
757                 my ($filter, $hops) =
758                         $self->{inannfilter}->it(@_, $self->{call},
759                                                                          @a[0..2],
760                                                                          @b[0..2], $a[3], $b[3]);
761                 unless ($filter) {
762                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
763                         return;
764                 }
765         }
766
767         if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
768                 my $dxchan = DXChannel::get($from);
769                 if ($self == $main::me && $dxchan && $dxchan->is_user) {
770                         if ($dxchan->priv < 5) {
771                                 $dxchan->send($dxchan->msg('dup'));
772                                 return;
773                         }
774                 } else {
775                         dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
776                         return;
777                 }
778         }
779
780
781         Log('chat', $target, $from, $text);
782
783         # send it if it isn't the except list and isn't isolated and still has a hop count
784         # taking into account filtering and so on
785         foreach $dxchan (@dxchan) {
786                 my $is_ak1a = $dxchan->is_ak1a;
787
788                 if ($dxchan->is_node) {
789                         next if $dxchan == $main::me;
790                         next if $dxchan == $self;
791                         next if $from_pc9x && $dxchan->{do_pc9x};
792                         next unless $dxchan->is_spider || $is_ak1a;
793                         next if $target eq 'LOCAL';
794                         if (!$ak1a_line && $is_ak1a) {
795                                 $ak1a_line = pc12($_[0], $text, $_[1], "$target.LST");
796                         }
797                 }
798
799                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
800                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
801         }
802 }
803
804 sub announce
805 {
806         my $self = shift;
807         my $line = shift;
808         my $isolate = shift;
809         my $to = shift;
810         my $target = shift;
811         my $text = shift;
812         my ($filter, $hops);
813
814         if ($self->{annfilter}) {
815                 ($filter, $hops) = $self->{annfilter}->it(@_);
816                 return unless $filter;
817         }
818         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
819 }
820
821 sub chat
822 {
823         my $self = shift;
824         my $line = shift;
825         my $isolate = shift;
826         my $to = shift;
827         my $target = shift;
828         my $text = shift;
829         my ($filter, $hops);
830
831         if ($self->{annfilter}) {
832                 ($filter, $hops) = $self->{annfilter}->it(@_);
833                 return unless $filter;
834         }
835         if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
836                 send_prot_line($self, $filter, $hops, $isolate, $line);
837         }
838 }
839
840
841 sub send_local_config
842 {
843         my $self = shift;
844
845         dbg('DXProt::send_local_config') if isdbg('trace');
846
847         # send our nodes
848         my $node;
849         my @nodes;
850         my @localnodes;
851         my @remotenodes;
852
853         if ($self->{isolate}) {
854                 dbg("send_local_config: isolated");
855                 @localnodes = ( $main::routeroot );
856                 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
857         } elsif ($self->{do_pc9x}) {
858                 dbg("send_local_config: doing pc9x");
859                 my $node = Route::Node::get($self->{call});
860                 $self->send_last_pc92_config($main::routeroot);
861                 $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
862         } else {
863                 # create a list of all the nodes that are not connected to this connection
864                 # and are not themselves isolated, this to make sure that isolated nodes
865                 # don't appear outside of this node
866
867                 dbg("send_local_config: traditional");
868
869                 # send locally connected nodes
870                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
871                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
872                 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
873
874                 my $node;
875                 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
876                 my @intcalls;
877                 foreach $node (@rawintcalls) {
878                         push @intcalls, $node if grep $_ && $node != $_, @intcalls;
879                 }
880                 my $ref = Route::Node::get($self->{call});
881                 my @rnodes = $ref->nodes;
882                 foreach $node (@intcalls) {
883                         push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
884                 }
885                 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
886         }
887
888         # get all the users connected on the above nodes and send them out
889         unless ($self->{do_pc9x}) {
890                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
891                         if ($node) {
892                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
893                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
894                         } else {
895                                 dbg("sent a null value") if isdbg('chanerr');
896                         }
897                 }
898         }
899 }
900
901 sub gen_my_pc92_config
902 {
903         my $node = shift;
904
905         if ($node->{call} eq $main::mycall) {
906                 clear_pc92_changes();           # remove any slugged data, we are generating it as now
907                 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
908                 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
909                 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
910                 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
911                 return pc92c($node, @localnodes);
912         } else {
913                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
914                 return pc92c($node, @rout);
915         }
916 }
917
918 sub send_last_pc92_config
919 {
920         my $self = shift;
921         my $node = shift;
922         if (my $l = $node->last_PC92C) {
923                 $self->send($l);
924         } else {
925                 $self->send_pc92_config($node);
926         }
927 }
928
929 sub send_pc92_config
930 {
931         my $self = shift;
932         my $node = shift;
933
934         dbg('DXProt::send_pc92_config') if isdbg('trace');
935
936         $node->last_PC92C(gen_my_pc92_config($node));
937         $self->send($node->last_PC92C);
938 }
939
940 sub broadcast_pc92_update
941 {
942         my $self = shift;
943         my $call = shift;
944
945         dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
946
947         my $nref = Route::Node::get($call);
948         unless ($nref) {
949                 dbg("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
950                 return;
951         }
952         my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
953         $nref->lastid(last_pc9x_id());
954         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
955         $self->update_pc92_next;
956 }
957
958 sub broadcast_pc92_keepalive
959 {
960         my $self = shift;
961         my $call = shift;
962
963         dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
964
965         my $nref = Route::Node::get($call);
966         unless ($nref) {
967                 dbg("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
968                 return;
969         }
970         my $l = pc92k($nref);
971         $nref->lastid(last_pc9x_id());
972         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
973         $self->update_pc92_keepalive;
974 }
975
976 sub time_out_pc92_routes
977 {
978         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
979         my @rdel;
980         foreach my $n (@nodes) {
981                 my $o = $n->dec_obs;
982                 if ($o <= 0) {
983                         if (my $dxchan = DXChannel::get($n->call)) {
984                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
985                                 $dxchan->disconnect;
986                                 next;
987                         }
988                         my @parents = map {Route::Node::get($_)} $n->parents;
989                         for (@parents) {
990                                 if ($_) {
991                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount")  if isdbg('obscount');
992                                         push @rdel, $n->del($_);
993                                 }
994                         }
995                 } else {
996                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
997                 }
998         }
999         for (@rdel) {
1000                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1001         }
1002 }
1003
1004 #
1005 # route a message down an appropriate interface for a callsign
1006 #
1007 # is called route(to, pcline);
1008 #
1009
1010 sub route
1011 {
1012         my ($self, $call, $line) = @_;
1013
1014         if (ref $self && $call eq $self->{call}) {
1015                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1016                 return;
1017         }
1018
1019         # always send it down the local interface if available
1020         my $dxchan = DXChannel::get($call);
1021         if ($dxchan) {
1022                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1023         } else {
1024                 my $cl = Route::get($call);
1025                 $dxchan = $cl->dxchan if $cl;
1026                 if (ref $dxchan) {
1027                         if (ref $self && $dxchan eq $self) {
1028                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1029                                 return;
1030                         }
1031                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1032                 }
1033         }
1034
1035         # try the backstop method
1036         unless ($dxchan) {
1037                 my $rcall = RouteDB::get($call);
1038                 if ($rcall) {
1039                         if ($self && $rcall eq $self->{call}) {
1040                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1041                                 return;
1042                         }
1043                         $dxchan = DXChannel::get($rcall);
1044                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
1045                 }
1046         }
1047
1048         if ($dxchan) {
1049                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1050                 if ($routeit) {
1051                         $dxchan->send($routeit) unless $dxchan == $main::me;
1052                 }
1053         } else {
1054                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1055         }
1056 }
1057
1058 #
1059 # obtain the hops from the list for this callsign and pc no
1060 #
1061
1062 sub get_hops
1063 {
1064         my $pcno = shift;
1065         my $hops = $DXProt::hopcount{$pcno};
1066         $hops = $DXProt::def_hopcount if !$hops;
1067         return "H$hops";
1068 }
1069
1070 #
1071 # adjust the hop count on a per node basis using the user loadable
1072 # hop table if available or else decrement an existing one
1073 #
1074
1075 sub adjust_hops
1076 {
1077         my $self = shift;
1078         my $s = shift;
1079         my $call = $self->{call};
1080         my $hops;
1081
1082         if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1083                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1084                 confess "$call called adjust_hops with '$s'" unless $pcno;
1085                 my $ref = $nodehops{$call} if %nodehops;
1086                 if ($ref) {
1087                         my $newhops = $ref->{$pcno};
1088                         return "" if defined $newhops && $newhops == 0;
1089                         $newhops = $ref->{default} unless $newhops;
1090                         return "" if defined $newhops && $newhops == 0;
1091                         $newhops = $hops unless $newhops;
1092                         return "" unless $newhops > 0;
1093                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1094                 } else {
1095                         return "" unless $hops > 0;
1096                 }
1097         }
1098         return $s;
1099 }
1100
1101 #
1102 # load hop tables
1103 #
1104 sub load_hops
1105 {
1106         my $self = shift;
1107         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1108         do "$main::data/hop_table.pl";
1109         return $@ if $@;
1110         return ();
1111 }
1112
1113 sub process_rcmd
1114 {
1115         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1116         if ($tonode eq $main::mycall) {
1117                 my $ref = DXUser->get_current($fromnode);
1118                 my $cref = Route::Node::get($fromnode);
1119                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1120                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1121                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1122                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1123                                 my $oldpriv = $self->{priv};
1124                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1125                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1126                                 $self->{priv} = $oldpriv;
1127                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1128                                 delete $self->{remotecmd};
1129                         } else {
1130                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1131                         }
1132                 } else {
1133                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1134                 }
1135         } else {
1136                 my $ref = DXUser->get_current($tonode);
1137                 if ($ref && $ref->is_clx) {
1138                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1139                 } else {
1140                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1141                 }
1142         }
1143 }
1144
1145 sub process_rcmd_reply
1146 {
1147         my ($self, $tonode, $fromnode, $user, $line) = @_;
1148         if ($tonode eq $main::mycall) {
1149                 my $s = $rcmds{$fromnode};
1150                 if ($s) {
1151                         my $dxchan = DXChannel::get($s->{call});
1152                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1153                         $ref->send($line) if $ref;
1154                         delete $rcmds{$fromnode} if !$dxchan;
1155                 } else {
1156                         # send unsolicited ones to the sysop
1157                         my $dxchan = DXChannel::get($main::myalias);
1158                         $dxchan->send($line) if $dxchan;
1159                 }
1160         } else {
1161                 my $ref = DXUser->get_current($tonode);
1162                 if ($ref && $ref->is_clx) {
1163                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1164                 } else {
1165                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1166                 }
1167         }
1168 }
1169
1170 sub send_rcmd_reply
1171 {
1172         my $self = shift;
1173         my $tonode = shift;
1174         my $fromnode = shift;
1175         my $user = shift;
1176         while (@_) {
1177                 my $line = shift;
1178                 $line =~ s/\s*$//;
1179                 Log('rcmd', 'out', $fromnode, $line);
1180                 if ($self->is_clx) {
1181                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1182                 } else {
1183                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1184                 }
1185         }
1186 }
1187
1188 # add a rcmd request to the rcmd queues
1189 sub addrcmd
1190 {
1191         my ($self, $to, $cmd) = @_;
1192
1193         my $r = {};
1194         $r->{call} = $self->{call};
1195         $r->{t} = $main::systime;
1196         $r->{cmd} = $cmd;
1197         $rcmds{$to} = $r;
1198
1199         my $ref = Route::Node::get($to);
1200         my $dxchan = $ref->dxchan;
1201         if ($dxchan && $dxchan->is_clx) {
1202                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1203         } else {
1204                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1205         }
1206 }
1207
1208 sub disconnect
1209 {
1210         my $self = shift;
1211         my $pc39flag = shift || 0;
1212         my $call = $self->call;
1213
1214         return if $self->{disconnecting}++;
1215
1216         unless ($pc39flag == 1) {
1217                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1218         }
1219
1220         # get rid of any PC16/17/19
1221         eph_del_regex("^PC1[679]*$call");
1222
1223         # do routing stuff, remove me from routing table
1224         my $node = Route::Node::get($call);
1225
1226         RouteDB::delete_interface($call);
1227
1228         # unbusy and stop and outgoing mail
1229         my $mref = DXMsg::get_busy($call);
1230         $mref->stop_msg($call) if $mref;
1231
1232         # remove outstanding pings
1233         delete $pings{$call};
1234
1235         # I was the last node visited
1236     $self->user->node($main::mycall);
1237
1238         # send info to all logged in thingies
1239         $self->tell_login('logoutn');
1240
1241         Log('DXProt', $call . " Disconnected");
1242
1243         $self->SUPER::disconnect;
1244
1245         # here we determine what needs to go out of the routing table
1246         my @rout;
1247         if ($node && $pc39flag != 2) {
1248                 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1249
1250                 @rout = $node->del($main::routeroot);
1251
1252                 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1253
1254                 # now we need to see what can't be routed anymore and came
1255                 # in via this node (probably).
1256                 my $n = 0;
1257                 while ($n != @rout) {
1258                         $n = @rout;
1259                         for (Route::Node::get_all()) {
1260                                 unless ($_->dxchan) {
1261                                         push @rout, $_->delete;
1262                                 }
1263                         }
1264                         dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1265                 }
1266
1267                 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1268
1269                 # and all my ephemera as well
1270                 for (@rout) {
1271                         my $c = $_->call;
1272                         eph_del_regex("^PC1[679].*$c");
1273                 }
1274         }
1275
1276         # broadcast to all other nodes that all the nodes connected to via me are gone
1277         unless ($pc39flag == 2)  {
1278                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1279                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1280         }
1281 }
1282
1283
1284 #
1285 # send a talk message to this thingy
1286 #
1287 sub talk
1288 {
1289         my ($self, $from, $to, $via, $line, $origin) = @_;
1290
1291         if ($self->{do_pc9x}) {
1292                 $self->send(pc93($to, $from, $via, $line));
1293         } else {
1294                 $self->send(pc10($from, $to, $via, $line, $origin));
1295         }
1296         Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1297 }
1298
1299 # send it if it isn't the except list and isn't isolated and still has a hop count
1300 # taking into account filtering and so on
1301
1302 sub send_route
1303 {
1304         my $self = shift;
1305         my $origin = shift;
1306         my $generate = shift;
1307         my $no = shift;     # the no of things to filter on
1308         my $routeit;
1309         my ($filter, $hops);
1310         my @rin;
1311
1312         for (; @_ && $no; $no--) {
1313                 my $r = shift;
1314
1315                 # don't send messages with $self's call in back to them
1316                 if ($r->call eq $self->{call}) {
1317                         dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1318                         next;
1319                 }
1320
1321                 if (!$self->{isolate} && $self->{routefilter}) {
1322                         $filter = undef;
1323                         if ($r) {
1324                                 ($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});
1325                                 if ($filter) {
1326                                         push @rin, $r;
1327                                 } else {
1328                                         dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1329                                 }
1330                         } else {
1331                                 dbg("was sent a null value") if isdbg('chanerr');
1332                         }
1333                 } else {
1334                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1335                 }
1336         }
1337         if (@rin) {
1338                 foreach my $line (&$generate(@rin, @_)) {
1339                         if ($hops) {
1340                                 $routeit = $line;
1341                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1342                         } else {
1343                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1344                                 next unless $routeit;
1345                         }
1346
1347                         $self->send($routeit);
1348                 }
1349         }
1350 }
1351
1352 # broadcast everywhere
1353 sub broadcast_route
1354 {
1355         my $self = shift;
1356         my $origin = shift;
1357         my $generate = shift;
1358         my $line = shift;
1359         my @dxchan = DXChannel::get_all_nodes();
1360         my $dxchan;
1361
1362         if ($line) {
1363                 $line =~ /\^H(\d+)\^?\~?$/;
1364                 return unless $1 > 0;
1365         }
1366         unless ($self->{isolate}) {
1367                 foreach $dxchan (@dxchan) {
1368                         next if $dxchan == $self || $dxchan == $main::me;
1369                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1370                         next unless $dxchan->isa('DXProt');
1371
1372                         $dxchan->send_route($origin, $generate, @_);
1373                 }
1374         }
1375 }
1376
1377 # broadcast to non-pc9x nodes
1378 sub broadcast_route_nopc9x
1379 {
1380         my $self = shift;
1381         my $origin = shift;
1382         my $generate = shift;
1383         my $line = shift;
1384         my @dxchan = DXChannel::get_all_nodes();
1385         my $dxchan;
1386
1387         if ($line) {
1388                 $line =~ /\^H(\d+)\^?\~?$/;
1389                 return unless $1 > 0;
1390         }
1391         unless ($self->{isolate}) {
1392                 foreach $dxchan (@dxchan) {
1393                         next if $dxchan == $self || $dxchan == $main::me;
1394                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1395                         next unless $dxchan->isa('DXProt');
1396                         next if $dxchan->{do_pc9x};
1397                         if ($generate == \&pc16 || $generate==\&pc17) {
1398                                 next unless $dxchan->user->wantsendpc16;
1399                         }
1400                         $dxchan->send_route($origin, $generate, @_);
1401                 }
1402         }
1403 }
1404
1405 # this is only used for next door nodes on init
1406 sub send_route_pc92
1407 {
1408         my $self = shift;
1409
1410         return unless $self->{do_pc9x};
1411
1412         my $origin = shift;
1413         my $generate = shift;
1414         my $no = shift;     # the no of things to filter on
1415         my $line;
1416
1417         $line = &$generate(@_);
1418         $self->send($line);
1419 }
1420
1421 # broadcast only to pc9x nodes
1422 sub broadcast_route_pc9x
1423 {
1424         my $self = shift;
1425         my $origin = shift;
1426         my $generate = shift;
1427         my $line = shift;
1428         my $no = shift;
1429         my @dxchan = DXChannel::get_all_nodes();
1430         my $dxchan;
1431
1432         if ($origin eq $main::mycall && $generate && !$line) {
1433                 $line = &$generate(@_);
1434         }
1435
1436         $line =~ /\^H(\d+)\^\~?$/;
1437         unless ($1 > 0 && $self->{isolate}) {
1438                 foreach $dxchan (@dxchan) {
1439                         next if $dxchan == $self || $dxchan == $main::me;
1440                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1441                         next unless $dxchan->isa('DXProt');
1442                         next unless $dxchan->{do_pc9x};
1443
1444                         $dxchan->send($line);
1445                 }
1446         }
1447 }
1448
1449 sub route_pc16
1450 {
1451         my $self = shift;
1452         return unless $self->user->wantpc16;
1453         my $origin = shift;
1454         my $line = shift;
1455         broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1456 }
1457
1458 sub route_pc17
1459 {
1460         my $self = shift;
1461         return unless $self->user->wantpc16;
1462         my $origin = shift;
1463         my $line = shift;
1464         broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1465 }
1466
1467 sub route_pc19
1468 {
1469         my $self = shift;
1470         my $origin = shift;
1471         my $line = shift;
1472         broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1473 }
1474
1475 sub route_pc21
1476 {
1477         my $self = shift;
1478         my $origin = shift;
1479         my $line = shift;
1480         broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1481 }
1482
1483 sub route_pc24
1484 {
1485         my $self = shift;
1486         my $origin = shift;
1487         my $line = shift;
1488         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1489 }
1490
1491 sub route_pc41
1492 {
1493         my $self = shift;
1494         my $origin = shift;
1495         my $line = shift;
1496         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1497 }
1498
1499 # this is probably obsolete now
1500 sub route_pc50
1501 {
1502         my $self = shift;
1503         my $origin = shift;
1504         my $line = shift;
1505
1506         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1507 }
1508
1509 sub route_pc92c
1510 {
1511         my $self = shift;
1512         my $origin = shift;
1513         my $line = shift;
1514         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1515 }
1516
1517 sub route_pc92a
1518 {
1519         my $self = shift;
1520         my $origin = shift;
1521         my $line = shift;
1522         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1523 }
1524
1525 sub route_pc92d
1526 {
1527         my $self = shift;
1528         my $origin = shift;
1529         my $line = shift;
1530         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1531 }
1532
1533 sub in_filter_route
1534 {
1535         my $self = shift;
1536         my $r = shift;
1537         my ($filter, $hops) = (1, 1);
1538
1539         if ($self->{inroutefilter}) {
1540                 ($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);
1541                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1542         }
1543         return $filter;
1544 }
1545
1546 sub eph_dup
1547 {
1548         my $s = shift;
1549         my $t = shift || $eph_restime;
1550         my $r;
1551
1552         # chop the end off
1553         $s =~ s/\^H\d\d?\^?\~?$//;
1554         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1555         $eph{$s} = $main::systime + $t;
1556         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1557         return $r;
1558 }
1559
1560 sub eph_del_regex
1561 {
1562         my $regex = shift;
1563         my ($key, $val);
1564         while (($key, $val) = each %eph) {
1565                 if ($key =~ m{$regex}) {
1566                         delete $eph{$key};
1567                 }
1568         }
1569 }
1570
1571 sub eph_clean
1572 {
1573         my ($key, $val);
1574
1575         while (($key, $val) = each %eph) {
1576                 if ($main::systime >= $val) {
1577                         delete $eph{$key};
1578                 }
1579         }
1580 }
1581
1582 sub eph_list
1583 {
1584         my ($key, $val);
1585         my @out;
1586
1587         while (($key, $val) = each %eph) {
1588                 push @out, $key, $val;
1589         }
1590         return @out;
1591 }
1592
1593 sub run_cmd
1594 {
1595         goto &DXCommandmode::run_cmd;
1596 }
1597
1598
1599 # import any msgs in the chat directory
1600 # the messages are sent to the chat group which forms the
1601 # the first part of the name (eg: solar.1243.txt would be
1602 # sent to chat group SOLAR)
1603 #
1604 # Each message found is sent: one non-blank line to one chat
1605 # message. So 4 lines = 4 chat messages.
1606 #
1607 # The special name LOCAL is for local users ANN
1608 # The special name ALL is for ANN/FULL
1609 # The special name SYSOP is for ANN/SYSOP
1610 #
1611 sub import_chat
1612 {
1613         # are there any to do in this directory?
1614         return unless -d $chatimportfn;
1615         unless (opendir(DIR, $chatimportfn)) {
1616                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1617                 Log('msg', "can\'t open $chatimportfn $!");
1618                 return;
1619         }
1620
1621         my @names = readdir(DIR);
1622         closedir(DIR);
1623         my $name;
1624         foreach $name (@names) {
1625                 next if $name =~ /^\./;
1626                 my $splitit = $name =~ /^split/;
1627                 my $fn = "$chatimportfn/$name";
1628                 next unless -f $fn;
1629                 unless (open(MSG, $fn)) {
1630                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1631                         Log('msg', "can\'t open import file $fn $!");
1632                         unlink($fn);
1633                         next;
1634                 }
1635                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1636                 close(MSG);
1637                 unlink($fn);
1638
1639                 my @cat = split /\./, $name;
1640                 my $target = uc $cat[0];
1641
1642                 foreach my $text (@msg) {
1643                         next unless $text && $text !~ /^\s*#/;
1644                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1645                                 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1646                                 my $wx = uc $target eq 'WX' ? '1' : '0';
1647                                 my $via = $target;
1648                                 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1649                                 Log('ann', $target, $main::mycall, $text);
1650                                 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1651                         } else {
1652                                 DXCommandmode::send_chats($main::me, $target, $text);
1653                         }
1654                 }
1655         }
1656 }
1657
1658 # start a pc92 find operation
1659 sub start_pc92_find
1660 {
1661         my $dxchan = shift;
1662         my $target = shift;
1663         my $key = "$dxchan->{call}|$target";
1664         if ($pc92_find{$key}) {
1665
1666         }
1667 }
1668
1669 # function (not method) to handle pc92 find returns
1670 sub handle_pc92_find_reply
1671 {
1672         my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1673
1674         $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');
1675 }
1676
1677 sub clean_pc92_find
1678 {
1679
1680 }
1681 1;
1682 __END__