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