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