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