Change DXUser->get* to DXUser::get*
[spider.git] / perl / DXProt.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 #
8 #
9
10 package DXProt;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXM;
18 use DXProtVars;
19 use DXCommandmode;
20 use DXLog;
21 use Spot;
22 use DXProtout;
23 use DXDebug;
24 use Filter;
25 use Local;
26 use DXDb;
27 use AnnTalk;
28 use Geomag;
29 use WCY;
30 use BadWords;
31 use DXHash;
32 use Route;
33 use Route::Node;
34 use Script;
35 use RouteDB;
36 use DXProtHandle;
37
38 use strict;
39
40 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
41                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
42                         $pingint $obscount %pc19list $chatdupeage $chatimportfn
43                         $pc19_version $myprot_version
44                         %nodehops $baddx $badspotter $badnode $censorpc
45                         $allowzero $decode_dk0wcy $send_opernam @checklist
46                         $eph_pc15_restime $pc92_update_period $pc92_obs_timeout
47                         %pc92_find $pc92_find_timeout $pc92_short_update_period
48                         $next_pc92_obs_timeout $pc92_slug_changes $last_pc92_slug
49                         $pc92_extnode_update_period $pc50_interval
50                         $pc92_keepalive_period
51                    );
52
53 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
54 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
55
56 $last_hour = time;                              # last time I did an hourly periodic update
57 %rcmds = ();                    # outstanding rcmd requests outbound
58 %nodehops = ();                 # node specific hop control
59 %pc19list = ();                                 # list of outstanding PC19s that haven't had PC16s on them
60
61 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
62                                                                 # loads of 'bad things'
63 $baddx = new DXHash "baddx";
64 $badspotter = new DXHash "badspotter";
65 $badnode = new DXHash "badnode";
66 $last10 = $last_pc50 = time;
67 $ann_to_talk = 1;
68 $eph_restime = 60;
69 $eph_info_restime = 60*60;
70 $eph_pc15_restime = 6*60;
71 $eph_pc34_restime = 30;
72 $pingint = 5*60;
73 $obscount = 2;
74 $chatdupeage = 20 * 60;
75 $chatimportfn = "$main::root/chat_import";
76 $pc19_version = 5454;                   # the visible version no for outgoing PC19s generated from pc59
77 $pc92_update_period = 4*60*60;  # the period between outgoing PC92 C updates
78 $pc92_short_update_period = 15*60; # shorten the update period after a connection or start up
79 $pc92_extnode_update_period = 1*60*60; # the update period for external nodes
80 $pc92_keepalive_period = 1*60*60;       # frequency of PC92 K (keepalive) records
81 %pc92_find = ();                                # outstanding pc92 find operations
82 $pc92_find_timeout = 30;                # maximum time to wait for a reply
83
84
85 @checklist =
86 (
87  [ qw(i c c m bp bc c) ],                       # pc10
88  [ qw(i f m d t m c c h) ],             # pc11
89  [ qw(i c bm m bm bm p h) ],            # pc12
90  [ qw(i c h) ],                                 #
91  [ qw(i c h) ],                                 #
92  [ qw(i c m h) ],                                       #
93  undef ,                                                # pc16 has to be validated manually
94  [ qw(i c c h) ],                                       # pc17
95  [ qw(i m n) ],                                 # pc18
96  undef ,                                                # pc19 has to be validated manually
97  undef ,                                                # pc20 no validation
98  [ qw(i c m h) ],                                       # pc21
99  undef ,                                                # pc22 no validation
100  [ qw(i d n n n n m c c h) ],           # pc23
101  [ qw(i c p h) ],                                       # pc24
102  [ qw(i c c n n) ],                             # pc25
103  [ qw(i f m d t m c c bc) ],            # pc26
104  [ qw(i d n n n n m c c bc) ],  # pc27
105  [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
106  [ qw(i c c n m) ],                             # pc29
107  [ qw(i c c n) ],                                       # pc30
108  [ qw(i c c n) ],                                       # pc31
109  [ qw(i c c n) ],                                       # pc32
110  [ qw(i c c n) ],                                       # pc33
111  [ qw(i c c m) ],                                       # pc34
112  [ qw(i c c m) ],                                       # pc35
113  [ qw(i c c m) ],                                       # pc36
114  [ qw(i c c n m) ],                             # pc37
115  undef,                                                 # pc38 not interested
116  [ qw(i c m) ],                                 # pc39
117  [ qw(i c c m p n) ],                           # pc40
118  [ qw(i c n m h) ],                             # pc41
119  [ qw(i c c n) ],                                       # pc42
120  undef,                                                 # pc43 don't handle it
121  [ qw(i c c n m m c) ],                 # pc44
122  [ qw(i c c n m) ],                             # pc45
123  [ qw(i c c n) ],                                       # pc46
124  undef,                                                 # pc47
125  undef,                                                 # pc48
126  [ qw(i c m h) ],                                       # pc49
127  [ qw(i c n h) ],                                       # pc50
128  [ qw(i c c n) ],                                       # pc51
129  undef,
130  undef,
131  undef,
132  undef,
133  undef,
134  undef,
135  undef,
136  undef,
137  undef,                                                 # pc60
138  undef,
139  undef,
140  undef,
141  undef,
142  undef,
143  undef,
144  undef,
145  undef,
146  undef,
147  undef,                                                 # pc70
148  undef,
149  undef,
150  [ qw(i d n n n n n n m m m c c h) ],   # pc73
151  undef,
152  undef,
153  undef,
154  undef,
155  undef,
156  undef,
157  undef,                                                 # pc80
158  undef,
159  undef,
160  undef,
161  [ qw(i c c c m) ],                             # pc84
162  [ qw(i c c c m) ],                             # pc85
163  undef,
164  undef,
165  undef,
166  undef,
167  undef,                                                 # pc90
168  undef,
169  [ qw(i c f l)],                                # pc92
170  [ qw(i c f *m c *c m)],                                        # pc93
171 );
172
173 # use the entry in the check list to check the field list presented
174 # return OK if line NOT in check list (for now)
175 sub check
176 {
177         my $n = shift;
178         $n -= 10;
179         return 0 if $n < 0 || $n > @checklist;
180         my $ref = $checklist[$n];
181         return 0 unless ref $ref;
182
183         my $i;
184         for ($i = 1; $i < @$ref; $i++) {
185                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
186                 return 0 unless $act;
187                 next if $blank eq 'b' && $_[$i] =~ /^[ \*]$/;
188                 next if $blank eq '*' && $_[$i] =~ /^\*$/;
189                 if ($act eq 'c') {
190                         return $i unless is_callsign($_[$i]);
191                 } elsif ($act eq 'i') {
192                         ;                                       # do nothing
193                 } elsif ($act eq 'm') {
194                         return $i unless is_pctext($_[$i]);
195                 } elsif ($act eq 'p') {
196                         return $i unless is_pcflag($_[$i]);
197                 } elsif ($act eq 'f') {
198                         return $i unless is_freq($_[$i]);
199                 } elsif ($act eq 'n') {
200                         return $i unless $_[$i] =~ /^[\d ]+$/;
201                 } elsif ($act eq 'h') {
202                         return $i unless $_[$i] =~ /^H\d\d?$/;
203                 } elsif ($act eq 'd') {
204                         return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
205                 } elsif ($act eq 't') {
206                         return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
207                 } elsif ($act eq 'l') {
208                         return $i unless $_[$i] =~ /^[A-Z]$/;
209                 }
210         }
211         return 0;
212 }
213
214 sub update_pc92_next
215 {
216         my $self = shift;
217         my $period = shift || ($self->{do_pc9x} ? $pc92_update_period : $pc92_extnode_update_period);
218         $self->{next_pc92_update} = $main::systime + $period - int rand($period / 4);
219         dbg("ROUTE: update_pc92_next: $self->{call} " . atime($self->{next_pc92_update})) if isdbg('obscount');
220 }
221
222 sub update_pc92_keepalive
223 {
224         my $self = shift;
225         my $period = shift || $pc92_keepalive_period;
226         $self->{next_pc92_keepalive} = $main::systime + $period - int rand($period / 4);
227         dbg("ROUTE: update_pc92_keepalive: $self->{call} " . atime($self->{next_pc92_keepalive})) if isdbg('obscount');
228 }
229
230 sub init
231 {
232         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
233         confess $@ if $@;
234
235         my $user = DXUser::get($main::mycall);
236         die "User $main::mycall not setup or disappeared RTFM" unless $user;
237
238         $myprot_version += $main::version*100;
239         $main::me = DXProt->new($main::mycall, 0, $user);
240         $main::me->{here} = 1;
241         $main::me->{state} = "indifferent";
242         $main::me->{sort} = 'S';    # S for spider
243         $main::me->{priv} = 9;
244         $main::me->{metric} = 0;
245         $main::me->{pingave} = 0;
246         $main::me->{registered} = 1;
247         $main::me->{version} = $main::version;
248         $main::me->{build} = "$main::subversion.$main::build";
249         $main::me->{do_pc9x} = 1;
250         $main::me->update_pc92_next($pc92_short_update_period);
251         $main::me->update_pc92_keepalive;
252 }
253
254 #
255 # obtain a new connection this is derived from dxchannel
256 #
257
258 sub new
259 {
260         my $self = DXChannel::alloc(@_);
261
262         # add this node to the table, the values get filled in later
263         my $pkg = shift;
264         my $call = shift;
265
266         # if we have an entry already, then send a PC21 to all connect
267         # old style connections, because we are about to get the real deal
268         if (my $ref = Route::Node::get($call)) {
269                 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
270                 my @rout = $ref->delete;
271                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
272         }
273         $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
274
275         return $self;
276 }
277
278 # this is how a pc connection starts (for an incoming connection)
279 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
280 # all the crap that comes between).
281 sub start
282 {
283         my ($self, $line, $sort) = @_;
284         my $call = $self->{call};
285         my $user = $self->{user};
286
287         # log it
288         my $host = $self->{conn}->{peerhost};
289         $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
290         $host ||= "unknown";
291
292         Log('DXProt', "$call connected from $host");
293
294         # remember type of connection
295         $self->{consort} = $line;
296         $self->{outbound} = $sort eq 'O';
297         my $priv = $user->priv;
298         $priv = $user->priv(1) unless $priv;
299         $self->{priv} = $priv;     # other clusters can always be 'normal' users
300         $self->{lang} = $user->lang || 'en';
301         $self->{isolate} = $user->{isolate};
302         $self->{consort} = $line;       # save the connection type
303         $self->{here} = 1;
304         $self->{width} = 80;
305
306         # sort out registration
307         $self->{registered} = 1;
308
309         # get the output filters
310         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
311         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
312         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
313         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
314         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
315
316
317         # get the INPUT filters (these only pertain to Clusters)
318         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
319         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
320         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
321         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
322         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
323         # if there is no route input filter then specify a default one.
324         # obviously this can be changed later by the sysop.
325         if (!$self->{inroutefilter}) {
326                 my $dxcc = $self->dxcc;
327                 $Route::filterdef->cmd($self, 'route', 'accept', "input by_dxcc $dxcc" );
328         }
329
330         # set unbuffered and no echo
331         $self->send_now('B',"0");
332         $self->send_now('E',"0");
333         $self->conn->echo(0) if $self->conn->can('echo');
334
335         # ping neighbour node stuff
336         my $ping = $user->pingint;
337         $ping = $pingint unless defined $ping;
338         $self->{pingint} = $ping;
339         $self->{nopings} = $user->nopings || $obscount;
340         $self->{pingtime} = [ ];
341         $self->{pingave} = 999;
342         $self->{metric} ||= 100;
343         $self->{lastping} = $main::systime;
344
345         # send initialisation string
346         unless ($self->{outbound}) {
347                 $self->sendinit;
348         }
349
350         $self->state('init');
351         $self->{pc50_t} = $main::systime;
352
353         # send info to all logged in thingies
354         $self->tell_login('loginn');
355
356         # run a script send the output to the debug file
357         my $script = new Script(lc $call) || new Script('node_default');
358         $script->run($self) if $script;
359
360         # set up a config broadcast "quite soon" to converge tables quicker
361         $main::me->update_pc92_next($pc92_short_update_period);
362         $self->update_pc92_next($pc92_short_update_period);
363
364         # set next keepalive time
365         $self->update_pc92_keepalive;
366 }
367
368 #
369 # send outgoing 'challenge'
370 #
371
372 sub sendinit
373 {
374         my $self = shift;
375         $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
376 }
377
378 #
379 # This is the normal pcxx despatcher
380 #
381 sub normal
382 {
383         my ($self, $line) = @_;
384
385         if ($line =~ '^<\w+\s' && $main::do_xml) {
386                 DXXml::normal($self, $line);
387                 return;
388         }
389
390         my @field = split /\^/, $line;
391         return unless @field;
392
393         pop @field if $field[-1] eq '~';
394
395 #       print join(',', @field), "\n";
396
397
398         # process PC frames, this will fail unless the frame starts PCnn
399         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
400         unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
401                 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
402                 return;
403         }
404
405         # check for and dump bad protocol messages
406         my $n = check($pcno, @field);
407         if ($n) {
408                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
409                 return;
410         }
411
412         # modify the hop count here
413         if ($self != $main::me) {
414                 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
415                         $trail ||= '';
416                         $hops--;
417                         return if $hops < 0;
418                         $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
419                         $field[-1] = "H$hops";
420                 }
421         }
422
423         if (defined &Local::pcprot) {
424                 my $r;
425                 eval { $r = Local::pcprot($self, $pcno, $line, @field); };
426                 return if $r;                   # i.e don't process it
427         }
428
429         # send it out for processing
430         my $origin = $self->{call};
431         no strict 'subs';
432         my $sub = "handle_$pcno";
433
434         if ($self->can($sub)) {
435                 $self->$sub($pcno, $line, $origin, @field);
436         } else {
437                 $self->handle_default($pcno, $line, $origin, @field);
438         }
439 }
440
441 #
442 # This is called from inside the main cluster processing loop and is used
443 # for despatching commands that are doing some long processing job
444 #
445 sub process
446 {
447         my $t = time;
448         my @dxchan = DXChannel::get_all();
449         my $dxchan;
450         my $pc50s;
451
452         # every ten seconds
453         if ($t - $last10 >= 10) {
454                 # clean out ephemera
455
456                 eph_clean();
457                 import_chat();
458
459                 $last10 = $t;
460
461                 # send out a pc50 on EVERY channel all at once
462                 if ($t >= $last_pc50 + $pc50_interval) {
463                         $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
464                         eph_dup($pc50s);
465                         $last_pc50 = $t;
466                 }
467
468                 foreach $dxchan (@dxchan) {
469                         next unless $dxchan->is_node;
470                         next if $dxchan == $main::me;
471
472                         # send the pc50
473                         $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
474
475                         # send a ping out on this channel
476                         if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
477                                 if ($dxchan->{nopings} <= 0) {
478                                         dbg("ROUTE: $dxchan->{call} disconnected on ping obscount") if isdbg('obscount');
479                                         $dxchan->disconnect;
480                                 } else {
481                                         DXXml::Ping::add($main::me, $dxchan->call);
482                                         $dxchan->{nopings} -= 1;
483                                         $dxchan->{lastping} = $t;
484                                         $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
485                                         dbg("ROUTE: $dxchan->{call} ping obscount = $dxchan->{nopings}") if isdbg('obscount');
486                                 }
487                         }
488                 }
489
490                 clean_pc92_find();
491
492                 # send out config broadcasts
493                 foreach $dxchan (@dxchan) {
494                         next unless $dxchan->is_node;
495
496                         # send out a PC92 config record if required for me and
497                         # all my non pc9x dependent nodes. But for dependent nodes we only do
498                         # this if we have not seen any from anyone else for at least half
499                         # of one update period. This should stop quite a bit of excess C
500                         # records. Someone will win, it does not really matter who, because
501                         # we always believe "us".
502                         if ($main::systime >= $dxchan->{next_pc92_update}) {
503                                 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
504                                         dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
505                                         my $ref = Route::Node::get($dxchan->{call});
506                                         if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_extnode_update_period/2)) {
507                                                 $dxchan->broadcast_pc92_update($dxchan->{call});
508                                         } else {
509                                                 $dxchan->update_pc92_next;
510                                         }
511                                 } else {
512                                         $dxchan->update_pc92_next; # this won't actually do anything, it's just to be tidy
513                                 }
514                         }
515                 }
516
517                 # do the keepalive for me, if required
518                 if ($main::systime >= $main::me->{next_pc92_keepalive}) {
519                         time_out_pc92_routes();
520                         $main::me->broadcast_pc92_keepalive($main::mycall);
521                 }
522
523                 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
524                         my ($add, $del) = gen_pc92_changes();
525                         $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
526                         $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
527                         clear_pc92_changes();
528                 }
529         }
530
531         if ($main::systime - 3600 > $last_hour) {
532                 $last_hour = $main::systime;
533         }
534 }
535
536 #
537 # finish up a pc context
538 #
539
540 #
541 # some active measures
542 #
543
544
545 sub send_dx_spot
546 {
547         my $self = shift;
548         my $line = shift;
549         my @dxchan = DXChannel::get_all();
550         my $dxchan;
551
552         # send it if it isn't the except list and isn't isolated and still has a hop count
553         # taking into account filtering and so on
554         foreach $dxchan (@dxchan) {
555                 next if $dxchan == $main::me;
556                 next if $dxchan == $self && $self->is_node;
557                 next if $line =~ /PC61/ && !$dxchan->is_spider && !$dxchan->is_user;
558                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
559         }
560 }
561
562 sub dx_spot
563 {
564         my $self = shift;
565         my $line = shift;
566         my $isolate = shift;
567         my ($filter, $hops);
568
569         if ($self->{spotsfilter}) {
570                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
571                 return unless $filter;
572         }
573         send_prot_line($self, $filter, $hops, $isolate, $line);
574 }
575
576 sub send_prot_line
577 {
578         my ($self, $filter, $hops, $isolate, $line) = @_;
579         my $routeit;
580
581
582         if ($hops) {
583                 $routeit = $line;
584                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
585         } else {
586                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
587                 return unless $routeit;
588         }
589         if ($filter) {
590                 $self->send($routeit);
591         } else {
592                 $self->send($routeit) unless $self->{isolate} || $isolate;
593         }
594 }
595
596
597 sub send_wwv_spot
598 {
599         my $self = shift;
600         my $line = shift;
601         my @dxchan = DXChannel::get_all();
602         my $dxchan;
603         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
604
605         # send it if it isn't the except list and isn't isolated and still has a hop count
606         # taking into account filtering and so on
607         foreach $dxchan (@dxchan) {
608                 next if $dxchan == $main::me;
609                 next if $dxchan == $self && $self->is_node;
610                 my $routeit;
611                 my ($filter, $hops);
612
613                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
614         }
615 }
616
617 sub wwv
618 {
619         my $self = shift;
620         my $line = shift;
621         my $isolate = shift;
622         my ($filter, $hops);
623
624         if ($self->{wwvfilter}) {
625                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
626                 return unless $filter;
627         }
628         send_prot_line($self, $filter, $hops, $isolate, $line)
629 }
630
631 sub send_wcy_spot
632 {
633         my $self = shift;
634         my $line = shift;
635         my @dxchan = DXChannel::get_all();
636         my $dxchan;
637         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
638
639         # send it if it isn't the except list and isn't isolated and still has a hop count
640         # taking into account filtering and so on
641         foreach $dxchan (@dxchan) {
642                 next if $dxchan == $main::me;
643                 next if $dxchan == $self;
644
645                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
646         }
647 }
648
649 sub wcy
650 {
651         my $self = shift;
652         my $line = shift;
653         my $isolate = shift;
654         my ($filter, $hops);
655
656         if ($self->{wcyfilter}) {
657                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
658                 return unless $filter;
659         }
660         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
661 }
662
663 # send an announce
664 sub send_announce
665 {
666         my $self = shift;
667         my $from_pc9x = shift;
668         my $line = shift;
669         my @dxchan = DXChannel::get_all();
670         my $dxchan;
671         my $target = $_[6];
672         my $to = 'To ';
673         my $text = unpad($_[2]);
674         my $from = $_[0];
675
676         if ($_[3] eq '*') {     # sysops
677                 $target = "SYSOP";
678         } elsif ($_[3] gt ' ') { # speciality list handling
679                 my ($name) = split /\./, $_[3];
680                 $target = "$name"; # put the rest in later (if bothered)
681         }
682
683         if ($_[5] eq '1') {
684                 $target = "WX";
685                 $to = '';
686         }
687         $target = "ALL" if !$target;
688
689
690         # obtain country codes etc
691         my @a = Prefix::cty_data($from);
692         my @b = Prefix::cty_data($_[4]);
693         if ($self->{inannfilter}) {
694                 my ($filter, $hops) =
695                         $self->{inannfilter}->it(@_, $self->{call},
696                                                                          @a[0..2],
697                                                                          @b[0..2], $a[3], $b[3]);
698                 unless ($filter) {
699                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
700                         return;
701                 }
702         }
703
704         # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
705         if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
706                 my $dxchan = DXChannel::get($from);
707                 if ($self == $main::me && $dxchan && $dxchan->is_user) {
708                         if ($dxchan->priv < 5) {
709                                 $dxchan->send($dxchan->msg('dup'));
710                                 return;
711                         }
712                 } else {
713                         dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
714                         return;
715                 }
716         }
717
718         Log('ann', $target, $from, $text);
719
720         # send it if it isn't the except list and isn't isolated and still has a hop count
721         # taking into account filtering and so on
722         foreach $dxchan (@dxchan) {
723                 next if $dxchan == $main::me;
724                 next if $dxchan == $self && $self->is_node;
725                 next if $from_pc9x && $dxchan->{do_pc9x};
726                 next if $target eq 'LOCAL' && $dxchan->is_node;
727                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
728                                                   @a[0..2], @b[0..2]);
729         }
730 }
731
732 my $msgid = int rand(1000);
733
734 sub nextchatmsgid
735 {
736         $msgid++;
737         $msgid = 1 if $msgid > 999;
738         return $msgid;
739 }
740
741 # send a chat line
742 sub send_chat
743 {
744         my $self = shift;
745         my $from_pc9x = shift;
746         my $line = shift;
747         my @dxchan = DXChannel::get_all();
748         my $dxchan;
749         my $target = $_[3];
750         my $text = unpad($_[2]);
751         my $ak1a_line;
752         my $from = $_[0];
753
754         # munge the group and recast the line if required
755         if ($target =~ s/\.LST$//) {
756                 $ak1a_line = $line;
757         }
758
759         # obtain country codes etc
760         my @a = Prefix::cty_data($from);
761         my @b = Prefix::cty_data($_[4]);
762         if ($self->{inannfilter}) {
763                 my ($filter, $hops) =
764                         $self->{inannfilter}->it(@_, $self->{call},
765                                                                          @a[0..2],
766                                                                          @b[0..2], $a[3], $b[3]);
767                 unless ($filter) {
768                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
769                         return;
770                 }
771         }
772
773         if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
774                 my $dxchan = DXChannel::get($from);
775                 if ($self == $main::me && $dxchan && $dxchan->is_user) {
776                         if ($dxchan->priv < 5) {
777                                 $dxchan->send($dxchan->msg('dup'));
778                                 return;
779                         }
780                 } else {
781                         dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
782                         return;
783                 }
784         }
785
786
787         Log('chat', $target, $from, $text);
788
789         # send it if it isn't the except list and isn't isolated and still has a hop count
790         # taking into account filtering and so on
791         foreach $dxchan (@dxchan) {
792                 my $is_ak1a = $dxchan->is_ak1a;
793
794                 if ($dxchan->is_node) {
795                         next if $dxchan == $main::me;
796                         next if $dxchan == $self;
797                         next if $from_pc9x && $dxchan->{do_pc9x};
798                         next unless $dxchan->is_spider || $is_ak1a;
799                         next if $target eq 'LOCAL';
800                         if (!$ak1a_line && $is_ak1a) {
801                                 $ak1a_line = pc12($_[0], $text, $_[1], "$target.LST");
802                         }
803                 }
804
805                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
806                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
807         }
808 }
809
810 sub announce
811 {
812         my $self = shift;
813         my $line = shift;
814         my $isolate = shift;
815         my $to = shift;
816         my $target = shift;
817         my $text = shift;
818         my ($filter, $hops);
819
820         if ($self->{annfilter}) {
821                 ($filter, $hops) = $self->{annfilter}->it(@_);
822                 return unless $filter;
823         }
824         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
825 }
826
827 sub chat
828 {
829         my $self = shift;
830         my $line = shift;
831         my $isolate = shift;
832         my $to = shift;
833         my $target = shift;
834         my $text = shift;
835         my ($filter, $hops);
836
837         if ($self->{annfilter}) {
838                 ($filter, $hops) = $self->{annfilter}->it(@_);
839                 return unless $filter;
840         }
841         if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
842                 send_prot_line($self, $filter, $hops, $isolate, $line);
843         }
844 }
845
846
847 sub send_local_config
848 {
849         my $self = shift;
850
851         dbg('DXProt::send_local_config') if isdbg('trace');
852
853         # send our nodes
854         my $node;
855         my @nodes;
856         my @localnodes;
857         my @remotenodes;
858
859         if ($self->{isolate}) {
860                 dbg("send_local_config: isolated");
861                 @localnodes = ( $main::routeroot );
862                 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
863         } elsif ($self->{do_pc9x}) {
864                 dbg("send_local_config: doing pc9x");
865                 my $node = Route::Node::get($self->{call});
866 #               $self->send_last_pc92_config($main::routeroot);
867 #               $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
868                 $self->send(pc92a($main::routeroot, $node));
869                 $self->send(pc92k($main::routeroot));
870         } else {
871                 # create a list of all the nodes that are not connected to this connection
872                 # and are not themselves isolated, this to make sure that isolated nodes
873                 # don't appear outside of this node
874
875                 dbg("send_local_config: traditional");
876
877                 # send locally connected nodes
878                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
879                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
880                 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
881
882                 my $node;
883                 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
884                 my @intcalls;
885                 foreach $node (@rawintcalls) {
886                         push @intcalls, $node if grep $_ && $node != $_, @intcalls;
887                 }
888                 my $ref = Route::Node::get($self->{call});
889                 my @rnodes = $ref->nodes;
890                 foreach $node (@intcalls) {
891                         push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
892                 }
893                 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
894         }
895
896         # get all the users connected on the above nodes and send them out
897         unless ($self->{do_pc9x}) {
898                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
899                         if ($node) {
900                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
901                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
902                         } else {
903                                 dbg("sent a null value") if isdbg('chanerr');
904                         }
905                 }
906         }
907 }
908
909 sub gen_my_pc92_config
910 {
911         my $node = shift;
912
913         if ($node->{call} eq $main::mycall) {
914                 clear_pc92_changes();           # remove any slugged data, we are generating it as now
915                 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
916                 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
917                 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
918                 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
919                 return pc92c($node, @localnodes);
920         } else {
921                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
922                 return pc92c($node, @rout);
923         }
924 }
925
926 sub send_last_pc92_config
927 {
928         my $self = shift;
929         my $node = shift;
930         if (my $l = $node->last_PC92C) {
931                 $self->send($l);
932         } else {
933                 $self->send_pc92_config($node);
934         }
935 }
936
937 sub send_pc92_config
938 {
939         my $self = shift;
940         my $node = shift;
941
942         dbg('DXProt::send_pc92_config') if isdbg('trace');
943
944         $node->last_PC92C(gen_my_pc92_config($node));
945         $self->send($node->last_PC92C);
946 }
947
948 sub broadcast_pc92_update
949 {
950         my $self = shift;
951         my $call = shift;
952
953         dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
954
955         my $nref = Route::Node::get($call);
956         unless ($nref) {
957                 dbg("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
958                 return;
959         }
960         my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
961         $nref->lastid(last_pc9x_id());
962         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
963         $self->update_pc92_next;
964 }
965
966 sub broadcast_pc92_keepalive
967 {
968         my $self = shift;
969         my $call = shift;
970
971         dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
972
973         my $nref = Route::Node::get($call);
974         unless ($nref) {
975                 dbg("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
976                 return;
977         }
978         my $l = pc92k($nref);
979         $nref->lastid(last_pc9x_id());
980         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
981         $self->update_pc92_keepalive;
982 }
983
984 sub time_out_pc92_routes
985 {
986         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
987         my @rdel;
988         foreach my $n (@nodes) {
989                 my $o = $n->dec_obs;
990                 if ($o <= 0) {
991                         if (my $dxchan = DXChannel::get($n->call)) {
992                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
993                                 $dxchan->disconnect;
994                                 next;
995                         }
996                         my @parents = map {Route::Node::get($_)} $n->parents;
997                         for (@parents) {
998                                 if ($_) {
999                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount")  if isdbg('obscount');
1000                                         push @rdel, $n->del($_);
1001                                 }
1002                         }
1003                 } else {
1004                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
1005                 }
1006         }
1007         for (@rdel) {
1008                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1009         }
1010 }
1011
1012 #
1013 # route a message down an appropriate interface for a callsign
1014 #
1015 # is called route(to, pcline);
1016 #
1017
1018 sub route
1019 {
1020         my ($self, $call, $line) = @_;
1021
1022         if (ref $self && $call eq $self->{call}) {
1023                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1024                 return;
1025         }
1026
1027         # always send it down the local interface if available
1028         my $dxchan = DXChannel::get($call);
1029         if ($dxchan) {
1030                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1031         } else {
1032                 my $cl = Route::get($call);
1033                 $dxchan = $cl->dxchan if $cl;
1034                 if (ref $dxchan) {
1035                         if (ref $self && $dxchan eq $self) {
1036                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1037                                 return;
1038                         }
1039                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1040                 }
1041         }
1042
1043         # try the backstop method
1044         unless ($dxchan) {
1045                 my $rcall = RouteDB::get($call);
1046                 if ($rcall) {
1047                         if ($self && $rcall eq $self->{call}) {
1048                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1049                                 return;
1050                         }
1051                         $dxchan = DXChannel::get($rcall);
1052                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
1053                 }
1054         }
1055
1056         if ($dxchan) {
1057                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1058                 if ($routeit) {
1059                         $dxchan->send($routeit) unless $dxchan == $main::me;
1060                 }
1061         } else {
1062                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1063         }
1064 }
1065
1066 #
1067 # obtain the hops from the list for this callsign and pc no
1068 #
1069
1070 sub get_hops
1071 {
1072         my $pcno = shift;
1073         my $hops = $DXProt::hopcount{$pcno};
1074         $hops = $DXProt::def_hopcount if !$hops;
1075         return "H$hops";
1076 }
1077
1078 #
1079 # adjust the hop count on a per node basis using the user loadable
1080 # hop table if available or else decrement an existing one
1081 #
1082
1083 sub adjust_hops
1084 {
1085         my $self = shift;
1086         my $s = shift;
1087         my $call = $self->{call};
1088         my $hops;
1089
1090         if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1091                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1092                 confess "$call called adjust_hops with '$s'" unless $pcno;
1093                 my $ref = $nodehops{$call} if %nodehops;
1094                 if ($ref) {
1095                         my $newhops = $ref->{$pcno};
1096                         return "" if defined $newhops && $newhops == 0;
1097                         $newhops = $ref->{default} unless $newhops;
1098                         return "" if defined $newhops && $newhops == 0;
1099                         $newhops = $hops unless $newhops;
1100                         return "" unless $newhops > 0;
1101                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1102                 } else {
1103                         return "" unless $hops > 0;
1104                 }
1105         }
1106         return $s;
1107 }
1108
1109 #
1110 # load hop tables
1111 #
1112 sub load_hops
1113 {
1114         my $self = shift;
1115         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1116         do "$main::data/hop_table.pl";
1117         return $@ if $@;
1118         return ();
1119 }
1120
1121 sub process_rcmd
1122 {
1123         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1124         if ($tonode eq $main::mycall) {
1125                 my $ref = DXUser::get_current($fromnode);
1126                 my $cref = Route::Node::get($fromnode);
1127                 Log('rcmd', 'in', ($ref->{priv}||0), $fromnode, $cmd);
1128                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $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         RouteDB::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__