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