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