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