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