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