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