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