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