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