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