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