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