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