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