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