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