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