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