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