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