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