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