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