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