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