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