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