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