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