1. make all newly learned nodes locked out by default.
[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 # $Id$
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 Time::HiRes qw(gettimeofday tv_interval);
31 use BadWords;
32 use DXHash;
33 use Route;
34 use Route::Node;
35
36 use strict;
37 use vars qw($me $pc11_max_age $pc23_max_age
38                         $last_hour $last10 %eph  %pings %rcmds
39                         %nodehops $baddx $badspotter $badnode $censorpc
40                         $allowzero $decode_dk0wcy $send_opernam @checklist);
41
42 $me = undef;                                    # the channel id for this cluster
43 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
44 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
45
46 $last_hour = time;                              # last time I did an hourly periodic update
47 %pings = ();                    # outstanding ping requests outbound
48 %rcmds = ();                    # outstanding rcmd requests outbound
49 %nodehops = ();                 # node specific hop control
50 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
51                                                                 # loads of 'bad things'
52 $baddx = new DXHash "baddx";
53 $badspotter = new DXHash "badspotter";
54 $badnode = new DXHash "badnode";
55 $last10 = time;
56
57 @checklist = 
58 (
59  [ qw(c c m bp bc c) ],                 # pc10
60  [ qw(f m d t m c c h) ],               # pc11
61  [ qw(c bc m bp bm p h) ],              # pc12
62  [ qw(c h) ],                                   # 
63  [ qw(c h) ],                                   # 
64  [ qw(c m h) ],                                 # 
65  undef ,                                                # pc16 has to be validated manually
66  [ qw(c c h) ],                                 # pc17
67  [ qw(m n) ],                                   # pc18
68  undef ,                                                # pc19 has to be validated manually
69  undef ,                                                # pc20 no validation
70  [ qw(c m h) ],                                 # pc21
71  undef ,                                                # pc22 no validation
72  [ qw(d n n n n m c c h) ],             # pc23
73  [ qw(c p h) ],                                 # pc24
74  [ qw(c c n n) ],                               # pc25
75  [ qw(f m d t m c c bc) ],              # pc26
76  [ qw(d n n n n m c c bc) ],    # pc27
77  [ qw(c c m c d t p m bp n p bp bc) ], # pc28
78  [ qw(c c n m) ],                               # pc29
79  [ qw(c c n) ],                                 # pc30
80  [ qw(c c n) ],                                 # pc31
81  [ qw(c c n) ],                                 # pc32
82  [ qw(c c n) ],                                 # pc33
83  [ qw(c c m) ],                                 # pc34
84  [ qw(c c m) ],                                 # pc35
85  [ qw(c c m) ],                                 # pc36
86  [ qw(c c n m) ],                               # pc37
87  undef,                                                 # pc38 not interested
88  [ qw(c m) ],                                   # pc39
89  [ qw(c c m p n) ],                             # pc40
90  [ qw(c n m h) ],                               # pc41
91  [ qw(c c n) ],                                 # pc42
92  undef,                                                 # pc43 don't handle it
93  [ qw(c c n m m c) ],                   # pc44
94  [ qw(c c n m) ],                               # pc45
95  [ qw(c c n) ],                                 # pc46
96  undef,                                                 # pc47
97  undef,                                                 # pc48
98  [ qw(c m h) ],                                 # pc49
99  [ qw(c n h) ],                                 # pc50
100  [ qw(c c n) ],                                 # pc51
101  undef,
102  undef,
103  undef,
104  undef,
105  undef,
106  undef,
107  undef,
108  undef,
109  undef,                                                 # pc60
110  undef,
111  undef,
112  undef,
113  undef,
114  undef,
115  undef,
116  undef,
117  undef,
118  undef,
119  undef,                                                 # pc70
120  undef,
121  undef,
122  [ qw(d n n n n n n m m m c c h) ],     # pc73
123  undef,
124  undef,
125  undef,
126  undef,
127  undef,
128  undef,
129  undef,                                                 # pc80
130  undef,
131  undef,
132  undef,
133  [ qw(c c c m) ],                               # pc84
134  [ qw(c c c m) ],                               # pc85
135 );
136
137 # use the entry in the check list to check the field list presented
138 # return OK if line NOT in check list (for now)
139 sub check
140 {
141         my $n = shift;
142         $n -= 10;
143         return 0 if $n < 0 || $n > @checklist; 
144         my $ref = $checklist[$n];
145         return 0 unless ref $ref;
146         
147         my $i;
148         shift;    # not interested in the first field
149         for ($i = 0; $i < @$ref; $i++) {
150                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
151                 return 0 unless $act;
152                 next if $blank && $_[$i] =~ /^[ \*]$/;
153                 if ($act eq 'c') {
154                         return $i+1 unless is_callsign($_[$i]);
155                 } elsif ($act eq 'm') {
156                         return $i+1 unless is_pctext($_[$i]);
157                 } elsif ($act eq 'p') {
158                         return $i+1 unless is_pcflag($_[$i]);
159                 } elsif ($act eq 'f') {
160                         return $i+1 unless is_freq($_[$i]);
161                 } elsif ($act eq 'n') {
162                         return $i+1 unless $_[$i] =~ /^[\d ]+$/;
163                 } elsif ($act eq 'h') {
164                         return $i+1 unless $_[$i] =~ /^H\d\d?$/;
165                 } elsif ($act eq 'd') {
166                         return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
167                 } elsif ($act eq 't') {
168                         return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
169                 }
170         }
171         return 0;
172 }
173
174 sub init
175 {
176         my $user = DXUser->get($main::mycall);
177         $DXProt::myprot_version += $main::version*100;
178         $me = DXProt->new($main::mycall, 0, $user); 
179         $me->{here} = 1;
180         $me->{state} = "indifferent";
181         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
182         confess $@ if $@;
183         $me->{sort} = 'S';    # S for spider
184         $me->{priv} = 9;
185 #       $Route::Node::me->adddxchan($me);
186 }
187
188 #
189 # obtain a new connection this is derived from dxchannel
190 #
191
192 sub new 
193 {
194         my $self = DXChannel::alloc(@_);
195
196         # add this node to the table, the values get filled in later
197         my $pkg = shift;
198         my $call = shift;
199         $main::routeroot->add($call, '0000', Route::here(1)) if $call ne $main::mycall;
200
201         return $self;
202 }
203
204 # this is how a pc connection starts (for an incoming connection)
205 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
206 # all the crap that comes between).
207 sub start
208 {
209         my ($self, $line, $sort) = @_;
210         my $call = $self->{call};
211         my $user = $self->{user};
212         
213         # remember type of connection
214         $self->{consort} = $line;
215         $self->{outbound} = $sort eq 'O';
216         my $priv = $user->priv;
217         $priv = $user->priv(1) unless $priv;
218         $self->{priv} = $priv;     # other clusters can always be 'normal' users
219         $self->{lang} = $user->lang || 'en';
220         $self->{isolate} = $user->{isolate};
221         $self->{consort} = $line;       # save the connection type
222         $self->{here} = 1;
223
224         # get the output filters
225         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
226         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
227         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
228         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
229         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) ;
230
231
232         # get the INPUT filters (these only pertain to Clusters)
233         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
234         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
235         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
236         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
237         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1);
238         
239         # set unbuffered and no echo
240         $self->send_now('B',"0");
241         $self->send_now('E',"0");
242         
243         # ping neighbour node stuff
244         my $ping = $user->pingint;
245         $ping = 5*60 unless defined $ping;
246         $self->{pingint} = $ping;
247         $self->{nopings} = $user->nopings || 2;
248         $self->{pingtime} = [ ];
249         $self->{pingave} = 0;
250
251         # send initialisation string
252         unless ($self->{outbound}) {
253                 $self->send(pc18());
254                 $self->{lastping} = $main::systime;
255         } else {
256                 $self->{lastping} = $main::systime + ($self->pingint / 2);
257         }
258         $self->state('init');
259         $self->{pc50_t} = $main::systime;
260
261         # send info to all logged in thingies
262         $self->tell_login('loginn');
263
264         Log('DXProt', "$call connected");
265 }
266
267 #
268 # This is the normal pcxx despatcher
269 #
270 sub normal
271 {
272         my ($self, $line) = @_;
273         my @field = split /\^/, $line;
274         return unless @field;
275         
276         pop @field if $field[-1] eq '~';
277         
278 #       print join(',', @field), "\n";
279                                                 
280         
281         # process PC frames, this will fail unless the frame starts PCnn
282         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
283         return unless $pcno;
284         return if $pcno < 10 || $pcno > 99;
285
286         # check for and dump bad protocol messages
287         my $n = check($pcno, @field);
288         if ($n) {
289                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
290                 return;
291         }
292
293         # local processing 1
294         my $pcr;
295         eval {
296                 $pcr = Local::pcprot($self, $pcno, @field);
297         };
298 #       dbg("Local::pcprot error $@") if isdbg('local') if $@;
299         return if $pcr;
300         
301  SWITCH: {
302                 if ($pcno == 10) {              # incoming talk
303
304                         # will we allow it at all?
305                         if ($censorpc) {
306                                 my @bad;
307                                 if (@bad = BadWords::check($field[3])) {
308                                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
309                                         return;
310                                 }
311                         }
312
313                         # is it for me or one of mine?
314                         my ($to, $via, $call, $dxchan);
315                         if ($field[5] gt ' ') {
316                                 $call = $via = $field[2];
317                                 $to = $field[5];
318                         } else {
319                                 $call = $to = $field[2];
320                         }
321                         $dxchan = DXChannel->get($main::myalias) if $call eq $main::mycall;
322                         $dxchan = DXChannel->get($call) unless $dxchan;
323                         if ($dxchan && $dxchan->is_user) {
324                                 $field[3] =~ s/\%5E/^/g;
325                                 $dxchan->talk($field[1], $to, $via, $field[3]);
326                         } else {
327                                 $self->route($field[2], $line); # relay it on its way
328                         }
329                         return;
330                 }
331                 
332                 if ($pcno == 11 || $pcno == 26) { # dx spot
333
334                         # route 'foreign' pc26s 
335                         if ($pcno == 26) {
336                                 if ($field[7] ne $main::mycall) {
337                                         $self->route($field[7], $line);
338                                         return;
339                                 }
340                         }
341                         
342                         # if this is a 'nodx' node then ignore it
343                         if ($badnode->in($field[7])) {
344                                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
345                                 return;
346                         }
347                         
348                         # if this is a 'bad spotter' user then ignore it
349                         if ($badspotter->in($field[6])) {
350                                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
351                                 return;
352                         }
353                         
354                         # convert the date to a unix date
355                         my $d = cltounix($field[3], $field[4]);
356                         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
357                         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
358                                 dbg("PCPROT: Spot ignored, invalid date or out of range ($field[3] $field[4])\n") if isdbg('chanerr');
359                                 return;
360                         }
361
362                         # is it 'baddx'
363                         if ($baddx->in($field[2])) {
364                                 dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
365                                 return;
366                         }
367                         
368                         # do some de-duping
369                         $field[5] =~ s/^\s+//;      # take any leading blanks off
370                         $field[2] = unpad($field[2]);   # take off leading and trailing blanks from spotted callsign
371                         if ($field[2] =~ /BUST\w*$/) {
372                                 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
373                                 return;
374                         }
375                         if (Spot::dup($field[1], $field[2], $d, $field[5])) {
376                                 dbg("PCPROT: Duplicate Spot ignored\n") if isdbg('chanerr');
377                                 return;
378                         }
379                         if ($censorpc) {
380                                 my @bad;
381                                 if (@bad = BadWords::check($field[5])) {
382                                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
383                                         return;
384                                 }
385                         }
386
387                         my @spot = Spot::prepare($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
388                         # global spot filtering on INPUT
389                         if ($self->{inspotsfilter}) {
390                                 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
391                                 unless ($filter) {
392                                         dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
393                                         return;
394                                 }
395                         }
396                         
397                         # add it 
398                         Spot::add(@spot);
399
400             #
401                         # @spot at this point contains:-
402             # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
403                         # then  spotted itu, spotted cq, spotters itu, spotters cq
404                         # you should be able to route on any of these
405             #
406                         
407                         # fix up qra locators of known users 
408                         my $user = DXUser->get_current($spot[4]);
409                         if ($user) {
410                                 my $qra = $user->qra;
411                                 unless ($qra && is_qra($qra)) {
412                                         my $lat = $user->lat;
413                                         my $long = $user->long;
414                                         if (defined $lat && defined $long) {
415                                                 $user->qra(DXBearing::lltoqra($lat, $long)); 
416                                                 $user->put;
417                                         }
418                                 }
419
420                                 # send a remote command to a distant cluster if it is visible and there is no
421                                 # qra locator and we havn't done it for a month.
422
423                                 unless ($user->qra) {
424                                         my $node;
425                                         my $to = $user->homenode;
426                                         my $last = $user->lastoper || 0;
427                                         if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
428                                                 my $cmd = "forward/opernam $spot[4]";
429                                                 # send the rcmd but we aren't interested in the replies...
430                                                 my $dxchan = $node->dxchan;
431                                                 if ($dxchan && $dxchan->is_clx) {
432                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
433                                                 } else {
434                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
435                                                 }
436                                                 if ($to ne $field[7]) {
437                                                         $to = $field[7];
438                                                         $node = Route::Node::get($to);
439                                                         if ($node) {
440                                                                 $dxchan = $node->dxchan;
441                                                                 if ($dxchan && $dxchan->is_clx) {
442                                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
443                                                                 } else {
444                                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
445                                                                 }
446                                                         }
447                                                 }
448                                                 $user->lastoper($main::systime);
449                                                 $user->put;
450                                         }
451                                 }
452                         }
453                                 
454                         # local processing 
455                         my $r;
456                         eval {
457                                 $r = Local::spot($self, @spot);
458                         };
459 #                       dbg("Local::spot1 error $@") if isdbg('local') if $@;
460                         return if $r;
461
462                         # DON'T be silly and send on PC26s!
463                         return if $pcno == 26;
464
465                         # send out the filtered spots
466                         send_dx_spot($self, $line, @spot) if @spot;
467                         return;
468                 }
469                 
470                 if ($pcno == 12) {              # announces
471                         # announce duplicate checking
472                         $field[3] =~ s/^\s+//;  # remove leading blanks
473                         if (AnnTalk::dup($field[1], $field[2], $field[3])) {
474                                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
475                                 return;
476                         }
477
478                         if ($censorpc) {
479                                 my @bad;
480                                 if (@bad = BadWords::check($field[3])) {
481                                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
482                                         return;
483                                 }
484                         }
485                         
486                         if ($field[2] eq '*' || $field[2] eq $main::mycall) {
487                                 
488                                 # global ann filtering on INPUT
489                                 if ($self->{inannfilter}) {
490                                         my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
491                                         my @dxcc = Prefix::extract($field[1]);
492                                         if (@dxcc > 0) {
493                                                 $ann_dxcc = $dxcc[1]->dxcc;
494                                                 $ann_itu = $dxcc[1]->itu;
495                                                 $ann_cq = $dxcc[1]->cq();                                               
496                                         }
497                                         @dxcc = Prefix::extract($field[5]);
498                                         if (@dxcc > 0) {
499                                                 $org_dxcc = $dxcc[1]->dxcc;
500                                                 $org_itu = $dxcc[1]->itu;
501                                                 $org_cq = $dxcc[1]->cq();                                               
502                                         }
503                                         my ($filter, $hops) = $self->{inannfilter}->it(@field[1..6], $self->{call}, 
504                                                                                                         $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
505                                         unless ($filter) {
506                                                 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
507                                                 return;
508                                         }
509                                 }
510
511                                 # send it
512                                 $self->send_announce($line, @field[1..6]);
513                         } else {
514                                 $self->route($field[2], $line);
515                         }
516                         
517                         return;
518                 }
519                 
520                 if ($pcno == 13) {
521                         last SWITCH;
522                 }
523                 if ($pcno == 14) {
524                         last SWITCH;
525                 }
526                 if ($pcno == 15) {
527                         last SWITCH;
528                 }
529                 
530                 if ($pcno == 16) {              # add a user
531
532                         # general checks
533                         my $dxchan;
534                         my $ncall = $field[1];
535                         my $newline = "PC16^";
536                         
537                         if ($ncall eq $main::mycall) {
538                                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
539                                 return;
540                         }
541                         $dxchan = DXChannel->get($ncall);
542                         if ($dxchan && $dxchan ne $self) {
543                                 dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
544                                 return;
545                         }
546                         my $parent = Route::Node::get($ncall); 
547                         unless ($parent) {
548                                 dbg("PCPROT: Node $ncall not in config") if isdbg('chanerr');
549                                 return;
550                         }
551                         
552                         # input filter if required
553                         return unless $self->in_filter_route($parent);
554                         
555                         my $i;
556                         my @rout;
557                         for ($i = 2; $i < $#field; $i++) {
558                                 my ($call, $conf, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
559                                 next unless $call && $conf && defined $here && is_callsign($call);
560                                 next if $call eq $main::mycall;
561
562                                 eph_del_regex("^PC17\^$call\^$ncall");
563                                 
564                                 $conf = $conf eq '*';
565
566                                 my $r = Route::User::get($call);
567                                 my $flags = Route::here($here)|Route::conf($conf);
568                                 
569                                 if ($r) {
570                                         if ($r->flags != $flags) {
571                                                 $r->flags($flags);
572                                                 push @rout, $r;
573                                         }
574                                         $r->addparent($ncall);
575                                 } else {
576                                         push @rout, $parent->add_user($call, $flags);
577                                 }
578                                 
579                                 # add this station to the user database, if required
580                                 $call =~ s/-\d+$//o;        # remove ssid for users
581                                 my $user = DXUser->get_current($call);
582                                 $user = DXUser->new($call) if !$user;
583                                 $user->homenode($parent->call) if !$user->homenode;
584                                 $user->node($parent->call);
585                                 $user->lastin($main::systime) unless DXChannel->get($call);
586                                 $user->put;
587                         }
588
589                         if (eph_dup($line)) {
590                                 dbg("PCPROT: dup PC16 detected") if isdbg('chanerr');
591                                 return;
592                         }
593                         
594                         # queue up any messages (look for privates only)
595                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
596
597                         $self->route_pc16($parent, @rout) if @rout;
598                         return;
599                 }
600                 
601                 if ($pcno == 17) {              # remove a user
602                         my $dxchan;
603                         my $ncall = $field[2];
604                         my $ucall = $field[1];
605
606                         eph_del_regex("^PC16.*$ncall.*$ucall");
607                         
608                         if ($ncall eq $main::mycall) {
609                                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
610                                 return;
611                         }
612                         $dxchan = DXChannel->get($ncall);
613                         if ($dxchan && $dxchan ne $self) {
614                                 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
615                                 return;
616                         }
617                         my $parent = Route::Node::get($ncall);
618                         unless ($parent) {
619                                 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
620                                 return;
621                         }
622
623                         # input filter if required
624                         return unless $self->in_filter_route($parent);
625
626                         my @rout = $parent->del_user($ucall);
627
628                         if (eph_dup($line)) {
629                                 dbg("PCPROT: dup PC17 detected") if isdbg('chanerr');
630                                 return;
631                         }
632
633                         $self->route_pc17($parent, @rout) if @rout;
634                         return;
635                 }
636                 
637                 if ($pcno == 18) {              # link request
638                         $self->state('init');   
639
640                         # first clear out any nodes on this dxchannel
641                         my $parent = Route::Node::get($self->{call});
642                         my @rout = $parent->del_nodes;
643                         $self->route_pc21(@rout, $parent);
644                         $self->send_local_config();
645                         $self->send(pc20());
646                         return;             # we don't pass these on
647                 }
648                 
649                 if ($pcno == 19) {              # incoming cluster list
650                         my $i;
651                         my $newline = "PC19^";
652
653                         # new routing list
654                         my @rout;
655                         my $parent = Route::Node::get($self->{call});
656                         unless ($parent) {
657                                 dbg("DXPROT: my parent $self->{call} has disappeared");
658                                 $self->disconnect;
659                                 return;
660                         }
661
662                         # parse the PC19
663                         for ($i = 1; $i < $#field-1; $i += 4) {
664                                 my $here = $field[$i];
665                                 my $call = uc $field[$i+1];
666                                 my $conf = $field[$i+2];
667                                 my $ver = $field[$i+3];
668                                 next unless defined $here && defined $conf && is_callsign($call);
669
670                                 eph_del_regex("^PC(?:21\^$call|17\^[^\^]+\^$call)");
671                                 
672                                 # check for sane parameters
673                                 $ver = 5000 if $ver eq '0000';
674                                 next if $ver < 5000; # only works with version 5 software
675                                 next if length $call < 3; # min 3 letter callsigns
676                                 next if $call eq $main::mycall;
677
678                                 # update it if required
679                                 my $r = Route::Node::get($call);
680                                 my $flags = Route::here($here)|Route::conf($conf);
681                                 if ($r) {
682                                         my $ar;
683                                         if ($call ne $parent->call) {
684                                                 if ($self->in_filter_route($r)) {
685                                                         $ar = $parent->add($call, $ver, $flags);
686                                                         push @rout, $ar if $ar;
687                                                 } else {
688                                                         next;
689                                                 }
690                                         }
691                                         if ($r->version ne $ver || $r->flags != $flags) {
692                                                 $r->version($ver);
693                                                 $r->flags($flags);
694                                                 push @rout, $r unless $ar;
695                                         }
696                                 } else {
697                                         if ($call eq $self->{call}) {
698                                                 dbg("DXPROT: my channel route for $call has disappeared");
699                                                 next;
700                                         };
701                                         
702                                         my $new = Route->new($call);          # throw away
703                                     if ($self->in_filter_route($new)) {
704                                                 my $r = $parent->add($call, $ver, $flags);
705                                                 push @rout, $r;
706                                         } else {
707                                                 next;
708                                         }
709                                 }
710
711                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
712                                 my $mref = DXMsg::get_busy($call);
713                                 $mref->stop_msg($call) if $mref;
714                                 
715                                 # add this station to the user database, if required (don't remove SSID from nodes)
716                                 my $user = DXUser->get_current($call);
717                                 if (!$user) {
718                                         $user = DXUser->new($call);
719                                         $user->sort('A');
720                                         $user->priv(1);                   # I have relented and defaulted nodes
721                                         $user->lockout(1);
722                                         $user->homenode($call);
723                                         $user->node($call);
724                                 }
725                                 $user->lastin($main::systime) unless DXChannel->get($call);
726                                 $user->put;
727                         }
728
729                         if (eph_dup($line)) {
730                                 dbg("PCPROT: dup PC19 detected") if isdbg('chanerr');
731                                 return;
732                         }
733
734                         $self->route_pc19(@rout) if @rout;
735                         return;
736                 }
737                 
738                 if ($pcno == 20) {              # send local configuration
739                         $self->send_local_config();
740                         $self->send(pc22());
741                         $self->state('normal');
742                         return;
743                 }
744                 
745                 if ($pcno == 21) {              # delete a cluster from the list
746                         my $call = uc $field[1];
747
748                         eph_del_regex("^PC1[79].*$call");
749                         
750                         my @rout;
751                         my $parent = Route::Node::get($self->{call});
752                         unless ($parent) {
753                                 dbg("DXPROT: my parent $self->{call} has disappeared");
754                                 $self->disconnect;
755                                 return;
756                         }
757                         my $node = Route::Node::get($call);
758                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
759                                 if ($call eq $self->{call}) {
760                                         dbg("PCPROT: Trying to disconnect myself with PC21") if isdbg('chanerr');
761                                         return;
762                                 }
763
764                                 if ($node) {
765                                         # input filter it
766                                         return unless $self->in_filter_route($node);
767
768                                         # routing objects
769                                         push @rout, $node->del($parent);
770                                 }
771                         } else {
772                                 dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
773                                 return;
774                         }
775
776                         if (eph_dup($line)) {
777                                 dbg("PCPROT: dup PC21 detected") if isdbg('chanerr');
778                                 return;
779                         }
780
781                         $self->route_pc21(@rout) if @rout;
782                         return;
783                 }
784                 
785                 if ($pcno == 22) {
786                         $self->state('normal');
787                         return;
788                 }
789                                 
790                 if ($pcno == 23 || $pcno == 27) { # WWV info
791                         
792                         # route 'foreign' pc27s 
793                         if ($pcno == 27) {
794                                 if ($field[8] ne $main::mycall) {
795                                         $self->route($field[8], $line);
796                                         return;
797                                 }
798                         }
799
800                         # do some de-duping
801                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
802                         my $sfi = unpad($field[3]);
803                         my $k = unpad($field[4]);
804                         my $i = unpad($field[5]);
805                         my ($r) = $field[6] =~ /R=(\d+)/;
806                         $r = 0 unless $r;
807                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
808                                 dbg("PCPROT: WWV Date ($field[1] $field[2]) out of range") if isdbg('chanerr');
809                                 return;
810                         }
811                         if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
812                                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
813                                 return;
814                         }
815                         $field[7] =~ s/-\d+$//o;            # remove spotter's ssid
816                 
817                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
818
819                         my $rep;
820                         eval {
821                                 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
822                         };
823 #                       dbg("Local::wwv2 error $@") if isdbg('local') if $@;
824                         return if $rep;
825
826                         # DON'T be silly and send on PC27s!
827                         return if $pcno == 27;
828
829                         # broadcast to the eager world
830                         send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
831                         return;
832                 }
833                 
834                 if ($pcno == 24) {              # set here status
835                         my $call = uc $field[1];
836                         my ($nref, $uref);
837                         $nref = Route::Node::get($call);
838                         $uref = Route::User::get($call);
839                         return unless $nref || $uref;   # if we don't know where they are, it's pointless sending it on
840                         
841                         unless (eph_dup($line)) {
842                                 $nref->here($field[2]) if $nref;
843                                 $uref->here($field[2]) if $uref;
844                                 my $ref = $nref || $uref;
845                                 return unless $self->in_filter_route($ref);
846                                 $self->route_pc24($ref, $field[3]);
847                         }
848                         return;
849                 }
850                 
851                 if ($pcno == 25) {      # merge request
852                         if ($field[1] ne $main::mycall) {
853                                 $self->route($field[1], $line);
854                                 return;
855                         }
856                         if ($field[2] eq $main::mycall) {
857                                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
858                                 return;
859                         }
860
861                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
862                         
863                         # spots
864                         if ($field[3] > 0) {
865                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
866                                 my $in;
867                                 foreach $in (@in) {
868                                         $self->send(pc26(@{$in}[0..4], $field[2]));
869                                 }
870                         }
871
872                         # wwv
873                         if ($field[4] > 0) {
874                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
875                                 my $in;
876                                 foreach $in (@in) {
877                                         $self->send(pc27(@{$in}[0..5], $field[2]));
878                                 }
879                         }
880                         return;
881                 }
882
883                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
884                         if ($pcno == 49 || $field[1] eq $main::mycall) {
885                                 DXMsg::process($self, $line);
886                         } else {
887                                 $self->route($field[1], $line) unless $self->is_clx;
888                         }
889                         return;
890                 }
891                 
892                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
893                         $self->process_rcmd($field[1], $field[2], $field[2], $field[3]);
894                         return;
895                 }
896                 
897                 if ($pcno == 35) {              # remote command replies
898                         $self->process_rcmd_reply($field[1], $field[2], $field[1], $field[3]);
899                         return;
900                 }
901                 
902                 # for pc 37 see 44 onwards
903
904                 if ($pcno == 38) {              # node connected list from neighbour
905                         return;
906                 }
907                 
908                 if ($pcno == 39) {              # incoming disconnect
909                         if ($field[1] eq $self->{call}) {
910                                 $self->disconnect(1);
911                                 eph_del_regex("^PC(?:1[679]|21).*$field[1]");
912                         } else {
913                                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
914                         }
915                         return;
916                 }
917                 
918                 if ($pcno == 41) {              # user info
919                         my $call = $field[1];
920
921                         # input filter if required
922 #                       my $ref = Route::get($call) || Route->new($call);
923 #                       return unless $self->in_filter_route($ref);
924
925                         if ($field[3] eq $field[2]) {
926                                 dbg('PCPROT: invalid value') if isdbg('chanerr');
927                                 return;
928                         }
929
930                         # add this station to the user database, if required
931                         my $user = DXUser->get_current($call);
932                         $user = DXUser->new($call) if !$user;
933                         
934                         if ($field[2] == 1) {
935                                 $user->name($field[3]);
936                         } elsif ($field[2] == 2) {
937                                 $user->qth($field[3]);
938                         } elsif ($field[2] == 3) {
939                                 if (is_latlong($field[3])) {
940                                         my ($lat, $long) = DXBearing::stoll($field[3]);
941                                         $user->lat($lat);
942                                         $user->long($long);
943                                         $user->qra(DXBearing::lltoqra($lat, $long));
944                                 } else {
945                                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
946                                         return;
947                                 }
948                         } elsif ($field[2] == 4) {
949                                 $user->homenode($field[3]);
950                         } elsif ($field[2] == 5) {
951                                 if (is_qra($field[3])) {
952                                         my ($lat, $long) = DXBearing::qratoll($field[3]);
953                                         $user->lat($lat);
954                                         $user->long($long);
955                                         $user->qra($field[3]);
956                                 } else {
957                                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
958                                         return;
959                                 }
960                         }
961                         $user->lastoper($main::systime);   # to cut down on excessive for/opers being generated
962                         $user->put;
963
964 #  perhaps this IS what we want after all
965 #                       $self->route_pc41($ref, $call, $field[2], $field[3], $field[4]);
966 #                       return;
967                 }
968
969                 if ($pcno == 43) {
970                         last SWITCH;
971                 }
972
973                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
974                         DXDb::process($self, $line);
975                         return;
976                 }
977                 
978                 if ($pcno == 50) {              # keep alive/user list
979                         my $call = $field[1];
980                         my $node = Route::Node::get($call);
981                         if ($node) {
982                                 return unless $node->call eq $self->{call};
983                                 $node->usercount($field[2]);
984
985                                 # input filter if required
986                                 return unless $self->in_filter_route($node);
987
988                                 $self->route_pc50($node, $field[2], $field[3]) unless eph_dup($line);
989                         }
990                         return;
991                 }
992                 
993                 if ($pcno == 51) {              # incoming ping requests/answers
994                         my $to = $field[1];
995                         my $from = $field[2];
996                         my $flag = $field[3];
997
998                         
999                         # is it for us?
1000                         if ($to eq $main::mycall) {
1001                                 if ($flag == 1) {
1002                                         $self->send(pc51($from, $to, '0'));
1003                                 } else {
1004                                         # it's a reply, look in the ping list for this one
1005                                         my $ref = $pings{$from};
1006                                         if ($ref) {
1007                                                 my $tochan =  DXChannel->get($from);
1008                                                 while (@$ref) {
1009                                                         my $r = shift @$ref;
1010                                                         my $dxchan = DXChannel->get($r->{call});
1011                                                         next unless $dxchan;
1012                                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
1013                                                         if ($dxchan->is_user) {
1014                                                                 my $s = sprintf "%.2f", $t; 
1015                                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
1016                                                                 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
1017                                                         } elsif ($dxchan->is_node) {
1018                                                                 if ($tochan) {
1019                                                                         $tochan->{nopings} = $tochan->user->nopings || 2; # pump up the timer
1020                                                                         push @{$tochan->{pingtime}}, $t;
1021                                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
1022                                                                         my $st;
1023                                                                         for (@{$tochan->{pingtime}}) {
1024                                                                                 $st += $_;
1025                                                                         }
1026                                                                         $tochan->{pingave} = $st / @{$tochan->{pingtime}};
1027                                                                 }
1028                                                         } 
1029                                                 }
1030                                         }
1031                                 }
1032                         } else {
1033                                 if (eph_dup($line)) {
1034                                         dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1035                                         return;
1036                                 }
1037                                 # route down an appropriate thingy
1038                                 $self->route($to, $line);
1039                         }
1040                         return;
1041                 }
1042
1043                 if ($pcno == 75) {              # dunno but route it
1044                         my $call = $field[1];
1045                         if ($call ne $main::mycall) {
1046                                 $self->route($call, $line);
1047                         }
1048                         return;
1049                 }
1050
1051                 if ($pcno == 73) {  # WCY broadcasts
1052                         my $call = $field[1];
1053                         
1054                         # do some de-duping
1055                         my $d = cltounix($call, sprintf("%02d18Z", $field[2]));
1056                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
1057                                 dbg("PCPROT: WCY Date ($call $field[2]) out of range") if isdbg('chanerr');
1058                                 return;
1059                         }
1060                         @field = map { unpad($_) } @field;
1061                         if (WCY::dup($d,@field[3..7])) {
1062                                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1063                                 return;
1064                         }
1065                 
1066                         my $wcy = WCY::update($d, @field[2..12]);
1067
1068                         my $rep;
1069                         eval {
1070                                 $rep = Local::wwv($self, @field[1..12]);
1071                         };
1072                         # dbg("Local::wcy error $@") if isdbg('local') if $@;
1073                         return if $rep;
1074
1075                         # broadcast to the eager world
1076                         send_wcy_spot($self, $line, $d, @field[2..12]);
1077                         return;
1078                 }
1079
1080                 if ($pcno == 84) { # remote commands (incoming)
1081                         $self->process_rcmd($field[1], $field[2], $field[3], $field[4]);
1082                         return;
1083                 }
1084
1085                 if ($pcno == 85) {              # remote command replies
1086                         $self->process_rcmd_reply($field[1], $field[2], $field[3], $field[4]);
1087                         
1088                         return;
1089                 }
1090         }
1091          
1092         # if get here then rebroadcast the thing with its Hop count decremented (if
1093         # there is one). If it has a hop count and it decrements to zero then don't
1094         # rebroadcast it.
1095         #
1096         # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1097         #        REBROADCAST!!!!
1098         #
1099
1100         if (eph_dup($line)) {
1101                 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1102         } else {
1103                 unless ($self->{isolate}) {
1104                         broadcast_ak1a($line, $self); # send it to everyone but me
1105                 }
1106         }
1107 }
1108
1109 #
1110 # This is called from inside the main cluster processing loop and is used
1111 # for despatching commands that are doing some long processing job
1112 #
1113 sub process
1114 {
1115         my $t = time;
1116         my @dxchan = DXChannel->get_all();
1117         my $dxchan;
1118         
1119         foreach $dxchan (@dxchan) {
1120                 next unless $dxchan->is_node();
1121                 next if $dxchan == $me;
1122                 
1123                 # send a pc50 out on this channel
1124                 $dxchan->{pc50_t} = $main::systime unless exists $dxchan->{pc50_t};
1125                 if ($t >= $dxchan->{pc50_t} + $DXProt::pc50_interval) {
1126                         my $s = pc50($me, scalar DXChannel::get_all_users);
1127                         eph_dup($s);
1128                         $dxchan->send($s);
1129                         $dxchan->{pc50_t} = $t;
1130                 } 
1131
1132                 # send a ping out on this channel
1133                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1134                         if ($dxchan->{nopings} <= 0) {
1135                                 $dxchan->disconnect;
1136                         } else {
1137                                 addping($main::mycall, $dxchan->call);
1138                                 $dxchan->{nopings} -= 1;
1139                                 $dxchan->{lastping} = $t;
1140                         }
1141                 }
1142         }
1143
1144         # every ten seconds
1145         if ($t - $last10 >= 10) {       
1146                 # clean out ephemera 
1147
1148                 eph_clean();
1149
1150                 $last10 = $t;
1151         }
1152         
1153         if ($main::systime - 3600 > $last_hour) {
1154                 $last_hour = $main::systime;
1155         }
1156 }
1157
1158 #
1159 # finish up a pc context
1160 #
1161
1162 #
1163 # some active measures
1164 #
1165
1166 sub send_dx_spot
1167 {
1168         my $self = shift;
1169         my $line = shift;
1170         my @dxchan = DXChannel->get_all();
1171         my $dxchan;
1172         
1173         # send it if it isn't the except list and isn't isolated and still has a hop count
1174         # taking into account filtering and so on
1175         foreach $dxchan (@dxchan) {
1176                 next if $dxchan == $me;
1177                 next if $dxchan == $self && $self->is_node;
1178                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
1179         }
1180 }
1181
1182 sub dx_spot
1183 {
1184         my $self = shift;
1185         my $line = shift;
1186         my $isolate = shift;
1187         my ($filter, $hops);
1188
1189         if ($self->{spotsfilter}) {
1190                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
1191                 return unless $filter;
1192         }
1193         send_prot_line($self, $filter, $hops, $isolate, $line);
1194 }
1195
1196 sub send_prot_line
1197 {
1198         my ($self, $filter, $hops, $isolate, $line) = @_;
1199         my $routeit;
1200         
1201         if ($hops) {
1202                 $routeit = $line;
1203                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1204         } else {
1205                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1206                 return unless $routeit;
1207         }
1208         if ($filter) {
1209                 $self->send($routeit);
1210         } else {
1211                 $self->send($routeit) unless $self->{isolate} || $isolate;
1212         }
1213 }
1214
1215
1216 sub send_wwv_spot
1217 {
1218         my $self = shift;
1219         my $line = shift;
1220         my @dxchan = DXChannel->get_all();
1221         my $dxchan;
1222         my ($wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1223         my @dxcc = Prefix::extract($_[7]);
1224         if (@dxcc > 0) {
1225                 $wwv_dxcc = $dxcc[1]->dxcc;
1226                 $wwv_itu = $dxcc[1]->itu;
1227                 $wwv_cq = $dxcc[1]->cq;                                         
1228         }
1229         @dxcc = Prefix::extract($_[8]);
1230         if (@dxcc > 0) {
1231                 $org_dxcc = $dxcc[1]->dxcc;
1232                 $org_itu = $dxcc[1]->itu;
1233                 $org_cq = $dxcc[1]->cq;                                         
1234         }
1235         
1236         # send it if it isn't the except list and isn't isolated and still has a hop count
1237         # taking into account filtering and so on
1238         foreach $dxchan (@dxchan) {
1239                 next if $dxchan == $me;
1240                 next if $dxchan == $self && $self->is_node;
1241                 my $routeit;
1242                 my ($filter, $hops);
1243
1244                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, $wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq);
1245         }
1246         
1247 }
1248
1249 sub wwv
1250 {
1251         my $self = shift;
1252         my $line = shift;
1253         my $isolate = shift;
1254         my ($filter, $hops);
1255         
1256         if ($self->{wwvfilter}) {
1257                 ($filter, $hops) = $self->{wwvfilter}->it(@_);
1258                 return unless $filter;
1259         }
1260         send_prot_line($self, $filter, $hops, $isolate, $line)
1261 }
1262
1263 sub send_wcy_spot
1264 {
1265         my $self = shift;
1266         my $line = shift;
1267         my @dxchan = DXChannel->get_all();
1268         my $dxchan;
1269         my ($wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1270         my @dxcc = Prefix::extract($_[11]);
1271         if (@dxcc > 0) {
1272                 $wcy_dxcc = $dxcc[1]->dxcc;
1273                 $wcy_itu = $dxcc[1]->itu;
1274                 $wcy_cq = $dxcc[1]->cq;                                         
1275         }
1276         @dxcc = Prefix::extract($_[12]);
1277         if (@dxcc > 0) {
1278                 $org_dxcc = $dxcc[1]->dxcc;
1279                 $org_itu = $dxcc[1]->itu;
1280                 $org_cq = $dxcc[1]->cq;                                         
1281         }
1282         
1283         # send it if it isn't the except list and isn't isolated and still has a hop count
1284         # taking into account filtering and so on
1285         foreach $dxchan (@dxchan) {
1286                 next if $dxchan == $me;
1287                 next if $dxchan == $self;
1288
1289                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, $wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq);
1290         }
1291 }
1292
1293 sub wcy
1294 {
1295         my $self = shift;
1296         my $line = shift;
1297         my $isolate = shift;
1298         my ($filter, $hops);
1299
1300         if ($self->{wcyfilter}) {
1301                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
1302                 return unless $filter;
1303         }
1304         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
1305 }
1306
1307 # send an announce
1308 sub send_announce
1309 {
1310         my $self = shift;
1311         my $line = shift;
1312         my @dxchan = DXChannel->get_all();
1313         my $dxchan;
1314         my $target;
1315         my $to = 'To ';
1316         my $text = unpad($_[2]);
1317                                 
1318         if ($_[3] eq '*') {     # sysops
1319                 $target = "SYSOP";
1320         } elsif ($_[3] gt ' ') { # speciality list handling
1321                 my ($name) = split /\./, $_[3]; 
1322                 $target = "$name"; # put the rest in later (if bothered) 
1323         } 
1324         
1325         if ($_[5] eq '1') {
1326                 $target = "WX"; 
1327                 $to = '';
1328         }
1329         $target = "ALL" if !$target;
1330         
1331         Log('ann', $target, $_[0], $text);
1332
1333         # obtain country codes etc 
1334         my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1335         my @dxcc = Prefix::extract($_[0]);
1336         if (@dxcc > 0) {
1337                 $ann_dxcc = $dxcc[1]->dxcc;
1338                 $ann_itu = $dxcc[1]->itu;
1339                 $ann_cq = $dxcc[1]->cq;                                         
1340         }
1341         @dxcc = Prefix::extract($_[4]);
1342         if (@dxcc > 0) {
1343                 $org_dxcc = $dxcc[1]->dxcc;
1344                 $org_itu = $dxcc[1]->itu;
1345                 $org_cq = $dxcc[1]->cq;                                         
1346         }
1347
1348         # send it if it isn't the except list and isn't isolated and still has a hop count
1349         # taking into account filtering and so on
1350         foreach $dxchan (@dxchan) {
1351                 next if $dxchan == $me;
1352                 next if $dxchan == $self && $self->is_node;
1353                 my $routeit;
1354                 my ($filter, $hops);
1355
1356                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq)
1357         }
1358 }
1359
1360 sub announce
1361 {
1362         my $self = shift;
1363         my $line = shift;
1364         my $isolate = shift;
1365         my $to = shift;
1366         my $target = shift;
1367         my ($filter, $hops);
1368
1369         if ($self->{annfilter}) {
1370                 ($filter, $hops) = $self->{annfilter}->it(@_);
1371                 return unless $filter;
1372         }
1373         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
1374 }
1375
1376
1377 sub send_local_config
1378 {
1379         my $self = shift;
1380         my $n;
1381         my @nodes;
1382         my @localnodes;
1383         my @remotenodes;
1384
1385         dbg('DXProt::send_local_config') if isdbg('trace');
1386         
1387         # send our nodes
1388         if ($self->{isolate}) {
1389                 @localnodes = ( $main::routeroot );
1390         } else {
1391                 # create a list of all the nodes that are not connected to this connection
1392                 # and are not themselves isolated, this to make sure that isolated nodes
1393         # don't appear outside of this node
1394                 my @dxchan = grep { $_->call ne $main::mycall && $_->call ne $self->{call} } DXChannel::get_all_nodes();
1395                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
1396                 my @intcalls = map { $_->nodes } @localnodes if @localnodes;
1397                 my $ref = Route::Node::get($self->{call});
1398                 my @rnodes = $ref->nodes;
1399                 for my $n (@intcalls) {
1400                         push @remotenodes, Route::Node::get($n) unless grep $n eq $_, @rnodes;
1401                 }
1402                 unshift @localnodes, $main::routeroot;
1403         }
1404         
1405         send_route($self, \&pc19, scalar(@localnodes)+scalar(@remotenodes), @localnodes, @remotenodes);
1406         
1407         # get all the users connected on the above nodes and send them out
1408         foreach $n (@localnodes, @remotenodes) {
1409                 if ($n) {
1410                         send_route($self, \&pc16, 1, $n, map {my $r = Route::User::get($_); $r ? ($r) : ()} $n->users);
1411                 } else {
1412                         dbg("sent a null value") if isdbg('chanerr');
1413                 }
1414         }
1415 }
1416
1417 #
1418 # route a message down an appropriate interface for a callsign
1419 #
1420 # is called route(to, pcline);
1421 #
1422 sub route
1423 {
1424         my ($self, $call, $line) = @_;
1425
1426         if (ref $self && $call eq $self->{call}) {
1427                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1428                 return;
1429         }
1430
1431         # always send it down the local interface if available
1432         my $dxchan = DXChannel->get($call);
1433         unless ($dxchan) {
1434                 my $cl = Route::get($call);
1435                 $dxchan = $cl->dxchan if $cl;
1436                 if (ref $dxchan) {
1437                         if (ref $self && $dxchan eq $self) {
1438                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1439                                 return;
1440                         }
1441                 }
1442         }
1443         if ($dxchan) {
1444                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1445                 if ($routeit) {
1446                         $dxchan->send($routeit) unless $dxchan == $me;
1447                 }
1448         } else {
1449                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1450         }
1451 }
1452
1453 # broadcast a message to all clusters taking into account isolation
1454 # [except those mentioned after buffer]
1455 sub broadcast_ak1a
1456 {
1457         my $s = shift;                          # the line to be rebroadcast
1458         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1459         my @dxchan = DXChannel::get_all_nodes();
1460         my $dxchan;
1461         
1462         # send it if it isn't the except list and isn't isolated and still has a hop count
1463         foreach $dxchan (@dxchan) {
1464                 next if grep $dxchan == $_, @except;
1465                 next if $dxchan == $me;
1466                 
1467                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1468                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1469         }
1470 }
1471
1472 # broadcast a message to all clusters ignoring isolation
1473 # [except those mentioned after buffer]
1474 sub broadcast_all_ak1a
1475 {
1476         my $s = shift;                          # the line to be rebroadcast
1477         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1478         my @dxchan = DXChannel::get_all_nodes();
1479         my $dxchan;
1480         
1481         # send it if it isn't the except list and isn't isolated and still has a hop count
1482         foreach $dxchan (@dxchan) {
1483                 next if grep $dxchan == $_, @except;
1484                 next if $dxchan == $me;
1485
1486                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1487                 $dxchan->send($routeit);
1488         }
1489 }
1490
1491 # broadcast to all users
1492 # storing the spot or whatever until it is in a state to receive it
1493 sub broadcast_users
1494 {
1495         my $s = shift;                          # the line to be rebroadcast
1496         my $sort = shift;           # the type of transmission
1497         my $fref = shift;           # a reference to an object to filter on
1498         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1499         my @dxchan = DXChannel::get_all_users();
1500         my $dxchan;
1501         my @out;
1502         
1503         foreach $dxchan (@dxchan) {
1504                 next if grep $dxchan == $_, @except;
1505                 push @out, $dxchan;
1506         }
1507         broadcast_list($s, $sort, $fref, @out);
1508 }
1509
1510 # broadcast to a list of users
1511 sub broadcast_list
1512 {
1513         my $s = shift;
1514         my $sort = shift;
1515         my $fref = shift;
1516         my $dxchan;
1517         
1518         foreach $dxchan (@_) {
1519                 my $filter = 1;
1520                 next if $dxchan == $me;
1521                 
1522                 if ($sort eq 'dx') {
1523                     next unless $dxchan->{dx};
1524                         ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1525                         next unless $filter;
1526                 }
1527                 next if $sort eq 'ann' && !$dxchan->{ann} && $s !~ /^To\s+LOCAL\s+de\s+(?:$main::myalias|$main::mycall)/i;
1528                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1529                 next if $sort eq 'wcy' && !$dxchan->{wcy};
1530                 next if $sort eq 'wx' && !$dxchan->{wx};
1531
1532                 $s =~ s/\a//og unless $dxchan->{beep};
1533
1534                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1535                         $dxchan->send($s);      
1536                 } else {
1537                         $dxchan->delay($s);
1538                 }
1539         }
1540 }
1541
1542
1543 #
1544 # obtain the hops from the list for this callsign and pc no 
1545 #
1546
1547 sub get_hops
1548 {
1549         my $pcno = shift;
1550         my $hops = $DXProt::hopcount{$pcno};
1551         $hops = $DXProt::def_hopcount if !$hops;
1552         return "H$hops";       
1553 }
1554
1555
1556 # adjust the hop count on a per node basis using the user loadable 
1557 # hop table if available or else decrement an existing one
1558 #
1559
1560 sub adjust_hops
1561 {
1562         my $self = shift;
1563         my $s = shift;
1564         my $call = $self->{call};
1565         my $hops;
1566         
1567         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1568                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1569                 confess "$call called adjust_hops with '$s'" unless $pcno;
1570                 my $ref = $nodehops{$call} if %nodehops;
1571                 if ($ref) {
1572                         my $newhops = $ref->{$pcno};
1573                         return "" if defined $newhops && $newhops == 0;
1574                         $newhops = $ref->{default} unless $newhops;
1575                         return "" if defined $newhops && $newhops == 0;
1576                         $newhops = $hops if !$newhops;
1577                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1578                 } else {
1579                         # simply decrement it
1580                         $hops--;
1581                         return "" if !$hops;
1582                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1583                 }
1584         }
1585         return $s;
1586 }
1587
1588
1589 # load hop tables
1590 #
1591 sub load_hops
1592 {
1593         my $self = shift;
1594         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1595         do "$main::data/hop_table.pl";
1596         return $@ if $@;
1597         return 0;
1598 }
1599
1600
1601 # add a ping request to the ping queues
1602 sub addping
1603 {
1604         my ($from, $to) = @_;
1605         my $ref = $pings{$to} || [];
1606         my $r = {};
1607         $r->{call} = $from;
1608         $r->{t} = [ gettimeofday ];
1609         route(undef, $to, pc51($to, $main::mycall, 1));
1610         push @$ref, $r;
1611         $pings{$to} = $ref;
1612 }
1613
1614 sub process_rcmd
1615 {
1616         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1617         if ($tonode eq $main::mycall) {
1618                 my $ref = DXUser->get_current($fromnode);
1619                 my $cref = Route::Node::get($fromnode);
1620                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1621                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1622                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1623                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1624                                 my $oldpriv = $self->{priv};
1625                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1626                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1627                                 $self->{priv} = $oldpriv;
1628                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1629                                 delete $self->{remotecmd};
1630                         } else {
1631                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1632                         }
1633                 } else {
1634                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1635                 }
1636         } else {
1637                 my $ref = DXUser->get_current($tonode);
1638                 if ($ref && $ref->is_clx) {
1639                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1640                 } else {
1641                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1642                 }
1643         }
1644 }
1645
1646 sub process_rcmd_reply
1647 {
1648         my ($self, $tonode, $fromnode, $user, $line) = @_;
1649         if ($tonode eq $main::mycall) {
1650                 my $s = $rcmds{$fromnode};
1651                 if ($s) {
1652                         my $dxchan = DXChannel->get($s->{call});
1653                         my $ref = $user eq $tonode ? $dxchan : (DXChannel->get($user) || $dxchan);
1654                         $ref->send($line) if $ref;
1655                         delete $rcmds{$fromnode} if !$dxchan;
1656                 } else {
1657                         # send unsolicited ones to the sysop
1658                         my $dxchan = DXChannel->get($main::myalias);
1659                         $dxchan->send($line) if $dxchan;
1660                 }
1661         } else {
1662                 my $ref = DXUser->get_current($tonode);
1663                 if ($ref && $ref->is_clx) {
1664                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1665                 } else {
1666                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1667                 }
1668         }
1669 }
1670
1671 sub send_rcmd_reply
1672 {
1673         my $self = shift;
1674         my $tonode = shift;
1675         my $fromnode = shift;
1676         my $user = shift;
1677         while (@_) {
1678                 my $line = shift;
1679                 $line =~ s/\s*$//;
1680                 Log('rcmd', 'out', $fromnode, $line);
1681                 if ($self->is_clx) {
1682                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1683                 } else {
1684                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1685                 }
1686         }
1687 }
1688
1689 # add a rcmd request to the rcmd queues
1690 sub addrcmd
1691 {
1692         my ($self, $to, $cmd) = @_;
1693
1694         my $r = {};
1695         $r->{call} = $self->{call};
1696         $r->{t} = $main::systime;
1697         $r->{cmd} = $cmd;
1698         $rcmds{$to} = $r;
1699         
1700         my $ref = Route::Node::get($to);
1701         my $dxchan = $ref->dxchan;
1702         if ($dxchan && $dxchan->is_clx) {
1703                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1704         } else {
1705                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1706         }
1707 }
1708
1709 sub disconnect
1710 {
1711         my $self = shift;
1712         my $pc39flag = shift;
1713         my $call = $self->call;
1714
1715         unless ($pc39flag && $pc39flag == 1) {
1716                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1717         }
1718
1719         # do routing stuff
1720         my $node = Route::Node::get($call);
1721         my @rout;
1722         if ($node) {
1723 #               @rout = $node->del_nodes;    # at the next level
1724                 @rout = $node->del($main::routeroot);
1725         }
1726         
1727         # unbusy and stop and outgoing mail
1728         my $mref = DXMsg::get_busy($call);
1729         $mref->stop_msg($call) if $mref;
1730         
1731         # broadcast to all other nodes that all the nodes connected to via me are gone
1732         unless ($pc39flag && $pc39flag == 2) {
1733                 $self->route_pc21(@rout) if @rout;
1734         }
1735
1736         # remove outstanding pings
1737         delete $pings{$call};
1738         
1739         # I was the last node visited
1740     $self->user->node($main::mycall);
1741
1742         # send info to all logged in thingies
1743         $self->tell_login('logoutn');
1744
1745         Log('DXProt', $call . " Disconnected");
1746
1747         $self->SUPER::disconnect;
1748 }
1749
1750
1751
1752 # send a talk message to this thingy
1753 #
1754 sub talk
1755 {
1756         my ($self, $from, $to, $via, $line) = @_;
1757         
1758         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1759         $self->send(DXProt::pc10($from, $to, $via, $line));
1760         Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);
1761 }
1762
1763 # send it if it isn't the except list and isn't isolated and still has a hop count
1764 # taking into account filtering and so on
1765 sub send_route
1766 {
1767         my $self = shift;
1768         my $generate = shift;
1769         my $no = shift;     # the no of things to filter on 
1770         my $routeit;
1771         my ($filter, $hops);
1772         my @rin;
1773         
1774         for (; @_ && $no; $no--) {
1775                 my $r = shift;
1776                 
1777                 if ($self->{routefilter}) {
1778                         $filter = undef;
1779                         if ($r) {
1780                                 ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq);
1781                                 if ($filter) {
1782                                         push @rin, $r;
1783                                 } else {
1784                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
1785                                 }
1786                         } else {
1787                                 dbg("was sent a null value") if isdbg('chanerr');
1788                         }
1789                 } else {
1790                         push @rin, $r;
1791                 }
1792         }
1793         if (@rin) {
1794                 foreach my $line (&$generate(@rin, @_)) {
1795                         if ($hops) {
1796                                 $routeit = $line;
1797                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1798                         } else {
1799                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1800                                 next unless $routeit;
1801                         }
1802                         $self->send($routeit);
1803                 }
1804         }
1805 }
1806
1807 sub broadcast_route
1808 {
1809         my $self = shift;
1810         my $generate = shift;
1811         my @dxchan = DXChannel::get_all_nodes();
1812         my $dxchan;
1813         my $line;
1814         
1815         foreach $dxchan (@dxchan) {
1816                 next if $dxchan == $self;
1817                 next if $dxchan == $me;
1818                 if ($dxchan->{routefilter}) {
1819                         $dxchan->send_route($generate, @_);
1820                 } else {
1821                         $dxchan->send_route($generate, @_) unless $self->{isolate} || $dxchan->{isolate};
1822                 }
1823         }
1824 }
1825
1826 sub route_pc16
1827 {
1828         my $self = shift;
1829         broadcast_route($self, \&pc16, 1, @_);
1830 }
1831
1832 sub route_pc17
1833 {
1834         my $self = shift;
1835         broadcast_route($self, \&pc17, 1, @_);
1836 }
1837
1838 sub route_pc19
1839 {
1840         my $self = shift;
1841         broadcast_route($self, \&pc19, scalar @_, @_);
1842 }
1843
1844 sub route_pc21
1845 {
1846         my $self = shift;
1847         broadcast_route($self, \&pc21, scalar @_, @_);
1848 }
1849
1850 sub route_pc24
1851 {
1852         my $self = shift;
1853         broadcast_route($self, \&pc24, 1, @_);
1854 }
1855
1856 sub route_pc41
1857 {
1858         my $self = shift;
1859         broadcast_route($self, \&pc41, 1, @_);
1860 }
1861
1862 sub route_pc50
1863 {
1864         my $self = shift;
1865         broadcast_route($self, \&pc50, 1, @_);
1866 }
1867
1868 sub in_filter_route
1869 {
1870         my $self = shift;
1871         my $r = shift;
1872         my ($filter, $hops) = (1, 1);
1873         
1874         if ($self->{inroutefilter}) {
1875                 ($filter, $hops) = $self->{inroutefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq);
1876                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1877         }
1878         return $filter;
1879 }
1880
1881 sub eph_dup
1882 {
1883         my $s = shift;
1884
1885         # chop the end off
1886         $s =~ s/\^H\d\d?\^?\~?$//;
1887         return 1 if exists $eph{$s};
1888         $eph{$s} = $main::systime;
1889         return undef;
1890 }
1891
1892 sub eph_del_regex
1893 {
1894         my $regex = shift;
1895         my ($key, $val);
1896         while (($key, $val) = each %eph) {
1897                 if ($key =~ m{$regex}) {
1898                         delete $eph{$key};
1899                 }
1900         }
1901 }
1902
1903 sub eph_clean
1904 {
1905         my ($key, $val);
1906         
1907         while (($key, $val) = each %eph) {
1908                 if ($main::systime - $val > 90) {
1909                         delete $eph{$key};
1910                 }
1911         }
1912 }
1913
1914 1;
1915 __END__