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