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