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