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