fae6dde07acb9e129179fc68d00063da0b9b4e0b
[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::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                 if ($dxchan->is_node) {
799                         next if $dxchan == $main::me;
800                         next if $dxchan == $self;
801                         next if $from_pc9x && $dxchan->do_pc9x;
802                         next unless $dxchan->is_spider && $dxchan->do_pc9x;
803                         next if $target eq 'LOCAL';
804                 }
805
806                 $dxchan->chat($line, $self->{isolate}, $target, $_[1],
807                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
808         }
809 }
810
811 sub announce
812 {
813         my $self = shift;
814         my $line = shift;
815         my $isolate = shift;
816         my $to = shift;
817         my $target = shift;
818         my $text = shift;
819         my ($filter, $hops);
820
821         if ($self->{annfilter}) {
822                 ($filter, $hops) = $self->{annfilter}->it(@_);
823                 return unless $filter;
824         }
825         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
826 }
827
828 sub chat
829 {
830         my $self = shift;
831         my $line = shift;
832         my $isolate = shift;
833         my $to = shift;
834         my $target = shift;
835         my $text = shift;
836         my ($filter, $hops);
837
838         if ($self->{annfilter}) {
839                 ($filter, $hops) = $self->{annfilter}->it(@_);
840                 return unless $filter;
841         }
842         if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
843                 send_prot_line($self, $filter, $hops, $isolate, $line);
844         }
845 }
846
847
848 sub send_local_config
849 {
850         my $self = shift;
851
852         dbg('DXProt::send_local_config') if isdbg('trace');
853
854         # send our nodes
855         my $node;
856         my @nodes;
857         my @localnodes;
858         my @remotenodes;
859
860         if ($self->{isolate}) {
861                 dbg("send_local_config: isolated");
862                 @localnodes = ( $main::routeroot );
863                 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
864         } elsif ($self->{do_pc9x}) {
865                 dbg("send_local_config: doing pc9x");
866                 my $node = Route::Node::get($self->{call});
867 #               $self->send_last_pc92_config($main::routeroot);
868 #               $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
869                 $self->send(pc92a($main::routeroot, $node));
870                 $self->send(pc92k($main::routeroot));
871         } else {
872                 # create a list of all the nodes that are not connected to this connection
873                 # and are not themselves isolated, this to make sure that isolated nodes
874                 # don't appear outside of this node
875
876                 dbg("send_local_config: traditional");
877
878                 # send locally connected nodes
879                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
880                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
881                 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
882
883                 my $node;
884                 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
885                 my @intcalls;
886                 foreach $node (@rawintcalls) {
887                         push @intcalls, $node if grep $_ && $node != $_, @intcalls;
888                 }
889                 my $ref = Route::Node::get($self->{call});
890                 my @rnodes = $ref->nodes;
891                 foreach $node (@intcalls) {
892                         push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
893                 }
894                 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
895         }
896
897         # get all the users connected on the above nodes and send them out
898         unless ($self->{do_pc9x}) {
899                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
900                         if ($node) {
901                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
902                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
903                         } else {
904                                 dbg("sent a null value") if isdbg('chanerr');
905                         }
906                 }
907         }
908 }
909
910 sub gen_my_pc92_config
911 {
912         my $node = shift;
913
914         if ($node->{call} eq $main::mycall) {
915                 clear_pc92_changes();           # remove any slugged data, we are generating it as now
916                 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
917                 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
918                 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
919                 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
920                 return pc92c($node, @localnodes);
921         } else {
922                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
923                 return pc92c($node, @rout);
924         }
925 }
926
927 sub send_last_pc92_config
928 {
929         my $self = shift;
930         my $node = shift;
931         if (my $l = $node->last_PC92C) {
932                 $self->send($l);
933         } else {
934                 $self->send_pc92_config($node);
935         }
936 }
937
938 sub send_pc92_config
939 {
940         my $self = shift;
941         my $node = shift;
942
943         dbg('DXProt::send_pc92_config') if isdbg('trace');
944
945         $node->last_PC92C(gen_my_pc92_config($node));
946         $self->send($node->last_PC92C);
947 }
948
949 sub broadcast_pc92_update
950 {
951         my $self = shift;
952         my $call = shift;
953
954         dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
955
956         my $nref = Route::Node::get($call);
957         unless ($nref) {
958                 cluck("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
959                 $self->update_pc92_next;
960                 return;
961         }
962         my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
963         $nref->lastid(last_pc9x_id());
964         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
965         $self->update_pc92_next;
966 }
967
968 sub broadcast_pc92_keepalive
969 {
970         my $self = shift;
971         my $call = shift;
972
973         dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
974
975         my $nref = Route::Node::get($call);
976         unless ($nref) {
977                 cluck("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
978                 $self->update_pc92_keepalive;
979                 return;
980         }
981         my $l = pc92k($nref);
982         $nref->lastid(last_pc9x_id());
983         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
984         $self->update_pc92_keepalive;
985 }
986
987 sub time_out_pc92_routes
988 {
989         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
990         my @rdel;
991         foreach my $n (@nodes) {
992                 my $o = $n->dec_obs;
993                 if ($o <= 0) {
994                         if (my $dxchan = DXChannel::get($n->call)) {
995                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
996                                 $dxchan->disconnect;
997                                 next;
998                         }
999                         my @parents = map {Route::Node::get($_)} $n->parents;
1000                         for (@parents) {
1001                                 if ($_) {
1002                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount")  if isdbg('obscount');
1003                                         push @rdel, $n->del($_);
1004                                 }
1005                         }
1006                 } else {
1007                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
1008                 }
1009         }
1010         for (@rdel) {
1011                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1012         }
1013 }
1014
1015 #
1016 # route a message down an appropriate interface for a callsign
1017 #
1018 # is called route(to, pcline);
1019 #
1020
1021 sub route
1022 {
1023         my ($self, $call, $line) = @_;
1024
1025         if (ref $self && $call eq $self->{call}) {
1026                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1027                 return;
1028         }
1029
1030         # always send it down the local interface if available
1031         my $dxchan = DXChannel::get($call);
1032         if ($dxchan) {
1033                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1034         } else {
1035                 my $cl = Route::get($call);
1036                 $dxchan = $cl->dxchan if $cl;
1037                 if (ref $dxchan) {
1038                         if (ref $self && $dxchan eq $self) {
1039                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1040                                 return;
1041                         }
1042                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1043                 }
1044         }
1045
1046         if ($dxchan) {
1047                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1048                 if ($routeit) {
1049                         $dxchan->send($routeit) unless $dxchan == $main::me;
1050                 }
1051         } else {
1052                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1053         }
1054 }
1055
1056 #
1057 # obtain the hops from the list for this callsign and pc no
1058 #
1059
1060 sub get_hops
1061 {
1062         my $pcno = shift;
1063         my $hops = $DXProt::hopcount{$pcno};
1064         $hops = $DXProt::def_hopcount if !$hops;
1065         return "H$hops";
1066 }
1067
1068 #
1069 # adjust the hop count on a per node basis using the user loadable
1070 # hop table if available or else decrement an existing one
1071 #
1072
1073 sub adjust_hops
1074 {
1075         my $self = shift;
1076         my $s = shift;
1077         my $call = $self->{call};
1078         my $hops;
1079
1080         if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1081                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1082                 confess "$call called adjust_hops with '$s'" unless $pcno;
1083                 my $ref = $nodehops{$call} if %nodehops;
1084                 if ($ref) {
1085                         my $newhops = $ref->{$pcno};
1086                         return "" if defined $newhops && $newhops == 0;
1087                         $newhops = $ref->{default} unless $newhops;
1088                         return "" if defined $newhops && $newhops == 0;
1089                         $newhops = $hops unless $newhops;
1090                         return "" unless $newhops > 0;
1091                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1092                 } else {
1093                         return "" unless $hops > 0;
1094                 }
1095         }
1096         return $s;
1097 }
1098
1099 #
1100 # load hop tables
1101 #
1102 sub load_hops
1103 {
1104         my $self = shift;
1105         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1106         do "$main::data/hop_table.pl";
1107         return $@ if $@;
1108         return ();
1109 }
1110
1111 sub process_rcmd
1112 {
1113         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1114         if ($tonode eq $main::mycall) {
1115                 my $ref = DXUser::get_current($fromnode);
1116                 unless ($ref && UNIVERSAL::isa($ref, 'DXUser')) {
1117                         dbg("DXProt process_rcmd: user $fromnode isn't a reference (check user_asc and tell G1TLH)"); 
1118                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1119                         return;
1120                 }
1121                 my $cref = Route::Node::get($fromnode);
1122                 unless ($cref && UNIVERSAL::isa($cref, 'Route')) {
1123                         dbg("DXProt process_rcmd: Route $fromnode isn't a reference (tell G1TLH)"); 
1124                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1125                         return;
1126                 }
1127                 Log('rcmd', 'in', ($ref->{priv}||0), $fromnode, $cmd);
1128                 if ($cmd !~ /^\s*rcmd/i && $ref->homenode && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1129                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1130                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1131                                 my $oldpriv = $self->{priv};
1132                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1133                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1134                                 $self->{priv} = $oldpriv;
1135                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1136                                 delete $self->{remotecmd};
1137                         } else {
1138                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1139                         }
1140                 } else {
1141                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1142                 }
1143         } else {
1144                 my $ref = DXUser::get_current($tonode);
1145                 if ($ref && $ref->is_clx) {
1146                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1147                 } else {
1148                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1149                 }
1150         }
1151 }
1152
1153 sub process_rcmd_reply
1154 {
1155         my ($self, $tonode, $fromnode, $user, $line) = @_;
1156         if ($tonode eq $main::mycall) {
1157                 my $s = $rcmds{$fromnode};
1158                 if ($s) {
1159                         my $dxchan = DXChannel::get($s->{call});
1160                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1161                         $ref->send($line) if $ref;
1162                         delete $rcmds{$fromnode} if !$dxchan;
1163                 } else {
1164                         # send unsolicited ones to the sysop
1165                         my $dxchan = DXChannel::get($main::myalias);
1166                         $dxchan->send($line) if $dxchan;
1167                 }
1168         } else {
1169                 my $ref = DXUser::get_current($tonode);
1170                 if ($ref && $ref->is_clx) {
1171                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1172                 } else {
1173                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1174                 }
1175         }
1176 }
1177
1178 sub send_rcmd_reply
1179 {
1180         my $self = shift;
1181         my $tonode = shift;
1182         my $fromnode = shift;
1183         my $user = shift;
1184         while (@_) {
1185                 my $line = shift;
1186                 $line =~ s/\s*$//;
1187                 Log('rcmd', 'out', $fromnode, $line);
1188                 if ($self->is_clx) {
1189                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1190                 } else {
1191                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1192                 }
1193         }
1194 }
1195
1196 # add a rcmd request to the rcmd queues
1197 sub addrcmd
1198 {
1199         my ($self, $to, $cmd) = @_;
1200
1201         my $r = {};
1202         $r->{call} = $self->{call};
1203         $r->{t} = $main::systime;
1204         $r->{cmd} = $cmd;
1205         $rcmds{$to} = $r;
1206
1207         my $ref = Route::Node::get($to);
1208         my $dxchan = $ref->dxchan;
1209         if ($dxchan && $dxchan->is_clx) {
1210                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1211         } else {
1212                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1213         }
1214 }
1215
1216 sub disconnect
1217 {
1218         my $self = shift;
1219         my $pc39flag = shift || 0;
1220         my $call = $self->call;
1221
1222         return if $self->{disconnecting}++;
1223
1224         unless ($pc39flag == 1) {
1225                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1226         }
1227
1228         # get rid of any PC16/17/19
1229         eph_del_regex("^PC1[679]*$call");
1230
1231         # do routing stuff, remove me from routing table
1232         my $node = Route::Node::get($call);
1233
1234         Route::delete_interface($call);
1235
1236         # unbusy and stop and outgoing mail
1237         my $mref = DXMsg::get_busy($call);
1238         $mref->stop_msg($call) if $mref;
1239
1240         # remove outstanding pings
1241         delete $pings{$call};
1242
1243         # I was the last node visited
1244     $self->user->node($main::mycall);
1245
1246         # send info to all logged in thingies
1247         $self->tell_login('logoutn');
1248
1249         Log('DXProt', $call . " Disconnected");
1250
1251         $self->SUPER::disconnect;
1252
1253         # here we determine what needs to go out of the routing table
1254         my @rout;
1255         if ($node && $pc39flag != 2) {
1256                 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1257
1258                 @rout = $node->del($main::routeroot);
1259
1260                 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1261
1262                 # now we need to see what can't be routed anymore and came
1263                 # in via this node (probably).
1264                 my $n = 0;
1265                 while ($n != @rout) {
1266                         $n = @rout;
1267                         for (Route::Node::get_all()) {
1268                                 unless ($_->dxchan) {
1269                                         push @rout, $_->delete;
1270                                 }
1271                         }
1272                         dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1273                 }
1274
1275                 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1276
1277                 # and all my ephemera as well
1278                 for (@rout) {
1279                         my $c = $_->call;
1280                         eph_del_regex("^PC1[679].*$c");
1281                 }
1282         }
1283
1284         # broadcast to all other nodes that all the nodes connected to via me are gone
1285         unless ($pc39flag == 2)  {
1286                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1287                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1288         }
1289 }
1290
1291
1292 #
1293 # send a talk message to this thingy
1294 #
1295 sub talk
1296 {
1297         my ($self, $from, $to, $via, $line, $origin) = @_;
1298
1299         if ($self->{do_pc9x}) {
1300                 $self->send(pc93($to, $from, $via, $line));
1301         } else {
1302                 $self->send(pc10($from, $to, $via, $line, $origin));
1303         }
1304         Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1305 }
1306
1307 # send it if it isn't the except list and isn't isolated and still has a hop count
1308 # taking into account filtering and so on
1309
1310 sub send_route
1311 {
1312         my $self = shift;
1313         my $origin = shift;
1314         my $generate = shift;
1315         my $no = shift;     # the no of things to filter on
1316         my $routeit;
1317         my ($filter, $hops);
1318         my @rin;
1319
1320         for (; @_ && $no; $no--) {
1321                 my $r = shift;
1322
1323                 # don't send messages with $self's call in back to them
1324                 if ($r->call eq $self->{call}) {
1325                         dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1326                         next;
1327                 }
1328
1329                 if (!$self->{isolate} && $self->{routefilter}) {
1330                         $filter = undef;
1331                         if ($r) {
1332                                 ($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});
1333                                 if ($filter) {
1334                                         push @rin, $r;
1335                                 } else {
1336                                         dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1337                                 }
1338                         } else {
1339                                 dbg("was sent a null value") if isdbg('chanerr');
1340                         }
1341                 } else {
1342                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1343                 }
1344         }
1345         if (@rin) {
1346                 foreach my $line (&$generate(@rin, @_)) {
1347                         if ($hops) {
1348                                 $routeit = $line;
1349                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1350                         } else {
1351                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1352                                 next unless $routeit;
1353                         }
1354
1355                         $self->send($routeit);
1356                 }
1357         }
1358 }
1359
1360 # broadcast everywhere
1361 sub broadcast_route
1362 {
1363         my $self = shift;
1364         my $origin = shift;
1365         my $generate = shift;
1366         my $line = shift;
1367         my @dxchan = DXChannel::get_all_nodes();
1368         my $dxchan;
1369
1370         if ($line) {
1371                 $line =~ /\^H(\d+)\^?\~?$/;
1372                 return unless $1 > 0;
1373         }
1374         unless ($self->{isolate}) {
1375                 foreach $dxchan (@dxchan) {
1376                         next if $dxchan == $self || $dxchan == $main::me;
1377                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1378                         next unless $dxchan->isa('DXProt');
1379
1380                         $dxchan->send_route($origin, $generate, @_);
1381                 }
1382         }
1383 }
1384
1385 # broadcast to non-pc9x nodes
1386 sub broadcast_route_nopc9x
1387 {
1388         my $self = shift;
1389         my $origin = shift;
1390         my $generate = shift;
1391         my $line = shift;
1392         my @dxchan = DXChannel::get_all_nodes();
1393         my $dxchan;
1394
1395         if ($line) {
1396                 $line =~ /\^H(\d+)\^?\~?$/;
1397                 return unless $1 > 0;
1398         }
1399         unless ($self->{isolate}) {
1400                 foreach $dxchan (@dxchan) {
1401                         next if $dxchan == $self || $dxchan == $main::me;
1402                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1403                         next unless $dxchan->isa('DXProt');
1404                         next if $dxchan->{do_pc9x};
1405                         if ($generate == \&pc16 || $generate==\&pc17) {
1406                                 next unless $dxchan->user->wantsendpc16;
1407                         }
1408                         $dxchan->send_route($origin, $generate, @_);
1409                 }
1410         }
1411 }
1412
1413 # this is only used for next door nodes on init
1414 sub send_route_pc92
1415 {
1416         my $self = shift;
1417
1418         return unless $self->{do_pc9x};
1419
1420         my $origin = shift;
1421         my $generate = shift;
1422         my $no = shift;     # the no of things to filter on
1423         my $line;
1424
1425         $line = &$generate(@_);
1426         $self->send($line);
1427 }
1428
1429 # broadcast only to pc9x nodes
1430 sub broadcast_route_pc9x
1431 {
1432         my $self = shift;
1433         my $origin = shift;
1434         my $generate = shift;
1435         my $line = shift;
1436         my $no = shift;
1437         my @dxchan = DXChannel::get_all_nodes();
1438         my $dxchan;
1439
1440         if ($origin eq $main::mycall && $generate && !$line) {
1441                 $line = &$generate(@_);
1442         }
1443
1444         $line =~ /\^H(\d+)\^\~?$/;
1445         unless ($1 > 0 && $self->{isolate}) {
1446                 foreach $dxchan (@dxchan) {
1447                         next if $dxchan == $self || $dxchan == $main::me;
1448                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1449                         next unless $dxchan->isa('DXProt');
1450                         next unless $dxchan->{do_pc9x};
1451
1452                         $dxchan->send($line);
1453                 }
1454         }
1455 }
1456
1457 sub route_pc16
1458 {
1459         my $self = shift;
1460         return unless $self->user->wantpc16;
1461         my $origin = shift;
1462         my $line = shift;
1463         broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1464 }
1465
1466 sub route_pc17
1467 {
1468         my $self = shift;
1469         return unless $self->user->wantpc16;
1470         my $origin = shift;
1471         my $line = shift;
1472         broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1473 }
1474
1475 sub route_pc19
1476 {
1477         my $self = shift;
1478         my $origin = shift;
1479         my $line = shift;
1480         broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1481 }
1482
1483 sub route_pc21
1484 {
1485         my $self = shift;
1486         my $origin = shift;
1487         my $line = shift;
1488         broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1489 }
1490
1491 sub route_pc24
1492 {
1493         my $self = shift;
1494         my $origin = shift;
1495         my $line = shift;
1496         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1497 }
1498
1499 sub route_pc41
1500 {
1501         my $self = shift;
1502         my $origin = shift;
1503         my $line = shift;
1504         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1505 }
1506
1507 # this is probably obsolete now
1508 sub route_pc50
1509 {
1510         my $self = shift;
1511         my $origin = shift;
1512         my $line = shift;
1513
1514         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1515 }
1516
1517 sub route_pc92c
1518 {
1519         my $self = shift;
1520         my $origin = shift;
1521         my $line = shift;
1522         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1523 }
1524
1525 sub route_pc92a
1526 {
1527         my $self = shift;
1528         my $origin = shift;
1529         my $line = shift;
1530         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1531 }
1532
1533 sub route_pc92d
1534 {
1535         my $self = shift;
1536         my $origin = shift;
1537         my $line = shift;
1538         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1539 }
1540
1541 sub in_filter_route
1542 {
1543         my $self = shift;
1544         my $r = shift;
1545         my ($filter, $hops) = (1, 1);
1546
1547         if ($self->{inroutefilter}) {
1548                 ($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);
1549                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1550         }
1551         return $filter;
1552 }
1553
1554 sub eph_dup
1555 {
1556         my $s = shift;
1557         my $t = shift || $eph_restime;
1558         my $r;
1559
1560         # chop the end off
1561         $s =~ s/\^H\d\d?\^?\~?$//;
1562         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1563         $eph{$s} = $main::systime + $t;
1564         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1565         return $r;
1566 }
1567
1568 sub eph_del_regex
1569 {
1570         my $regex = shift;
1571         my ($key, $val);
1572         while (($key, $val) = each %eph) {
1573                 if ($key =~ m{$regex}) {
1574                         delete $eph{$key};
1575                 }
1576         }
1577 }
1578
1579 sub eph_clean
1580 {
1581         my ($key, $val);
1582
1583         while (($key, $val) = each %eph) {
1584                 if ($main::systime >= $val) {
1585                         delete $eph{$key};
1586                 }
1587         }
1588 }
1589
1590 sub eph_list
1591 {
1592         my ($key, $val);
1593         my @out;
1594
1595         while (($key, $val) = each %eph) {
1596                 push @out, $key, $val;
1597         }
1598         return @out;
1599 }
1600
1601 sub run_cmd
1602 {
1603         goto &DXCommandmode::run_cmd;
1604 }
1605
1606
1607 # import any msgs in the chat directory
1608 # the messages are sent to the chat group which forms the
1609 # the first part of the name (eg: solar.1243.txt would be
1610 # sent to chat group SOLAR)
1611 #
1612 # Each message found is sent: one non-blank line to one chat
1613 # message. So 4 lines = 4 chat messages.
1614 #
1615 # The special name LOCAL is for local users ANN
1616 # The special name ALL is for ANN/FULL
1617 # The special name SYSOP is for ANN/SYSOP
1618 #
1619 sub import_chat
1620 {
1621         # are there any to do in this directory?
1622         return unless -d $chatimportfn;
1623         unless (opendir(DIR, $chatimportfn)) {
1624                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1625                 Log('msg', "can\'t open $chatimportfn $!");
1626                 return;
1627         }
1628
1629         my @names = readdir(DIR);
1630         closedir(DIR);
1631         my $name;
1632         foreach $name (@names) {
1633                 next if $name =~ /^\./;
1634                 my $splitit = $name =~ /^split/;
1635                 my $fn = "$chatimportfn/$name";
1636                 next unless -f $fn;
1637                 unless (open(MSG, $fn)) {
1638                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1639                         Log('msg', "can\'t open import file $fn $!");
1640                         unlink($fn);
1641                         next;
1642                 }
1643                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1644                 close(MSG);
1645                 unlink($fn);
1646
1647                 my @cat = split /\./, $name;
1648                 my $target = uc $cat[0];
1649
1650                 foreach my $text (@msg) {
1651                         next unless $text && $text !~ /^\s*#/;
1652                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1653                                 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1654                                 my $wx = uc $target eq 'WX' ? '1' : '0';
1655                                 my $via = $target;
1656                                 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1657                                 Log('ann', $target, $main::mycall, $text);
1658                                 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1659                         } else {
1660                                 DXCommandmode::send_chats($main::me, $target, $text);
1661                         }
1662                 }
1663         }
1664 }
1665
1666 # start a pc92 find operation
1667 sub start_pc92_find
1668 {
1669         my $dxchan = shift;
1670         my $target = shift;
1671         my $key = "$dxchan->{call}|$target";
1672         if ($pc92_find{$key}) {
1673
1674         }
1675 }
1676
1677 # function (not method) to handle pc92 find returns
1678 sub handle_pc92_find_reply
1679 {
1680         my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1681
1682         $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');
1683 }
1684
1685 sub clean_pc92_find
1686 {
1687
1688 }
1689 1;
1690 __END__