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