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