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