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