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