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