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