don't allow mail message routing for clx
[spider.git] / perl / DXProt.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 # $Id$
8
9
10 package DXProt;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXM;
18 use DXCluster;
19 use DXProtVars;
20 use DXCommandmode;
21 use DXLog;
22 use Spot;
23 use DXProtout;
24 use DXDebug;
25 use Filter;
26 use Local;
27 use DXDb;
28 use AnnTalk;
29 use Geomag;
30 use WCY;
31 use Time::HiRes qw(gettimeofday tv_interval);
32
33 use strict;
34 use vars qw($me $pc11_max_age $pc23_max_age
35                         $last_hour %pings %rcmds
36                         %nodehops @baddx $baddxfn 
37                         $allowzero $decode_dk0wcy $send_opernam @checklist);
38
39 $me = undef;                                    # the channel id for this cluster
40 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
41 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
42
43 $last_hour = time;                              # last time I did an hourly periodic update
44 %pings = ();                    # outstanding ping requests outbound
45 %rcmds = ();                    # outstanding rcmd requests outbound
46 %nodehops = ();                 # node specific hop control
47 @baddx = ();                    # list of illegal spotted callsigns
48
49
50 $baddxfn = "$main::data/baddx.pl";
51
52 @checklist = 
53 (
54  [ qw(c c m p bc c) ],                  # pc10
55  [ qw(f m d t m c c h) ],               # pc11
56  [ qw(c bc m bp bm p h) ],              # pc12
57  [ qw(c h) ],                                   # 
58  [ qw(c h) ],                                   # 
59  [ qw(c m h) ],                                 # 
60  undef ,                                                # pc16 has to be validated manually
61  [ qw(c c h) ],                                 # pc17
62  [ qw(m n) ],                                   # pc18
63  undef ,                                                # pc19 has to be validated manually
64  undef ,                                                # pc20 no validation
65  [ qw(c m h) ],                                 # pc21
66  undef ,                                                # pc22 no validation
67  [ qw(d n n n n m c c h) ],             # pc23
68  [ qw(c p h) ],                                 # pc24
69  [ qw(c c n n) ],                               # pc25
70  [ qw(f m d t m c c bc) ],              # pc26
71  [ qw(d n n n n m c c bc) ],    # pc27
72  [ qw(c c c c d t p m bp n p bp bc) ], # pc28
73  [ qw(c c n m) ],                               # pc29
74  [ qw(c c n) ],                                 # pc30
75  [ qw(c c n) ],                                 # pc31
76  [ qw(c c n) ],                                 # pc32
77  [ qw(c c n) ],                                 # pc33
78  [ qw(c c m) ],                                 # pc34
79  [ qw(c c m) ],                                 # pc35
80  [ qw(c c m) ],                                 # pc36
81  [ qw(c c n m) ],                               # pc37
82  undef,                                                 # pc38 not interested
83  [ qw(c m) ],                                   # pc39
84  [ qw(c c m p n) ],                             # pc40
85  [ qw(c n m h) ],                               # pc41
86  [ qw(c c n) ],                                 # pc42
87  undef,                                                 # pc43 don't handle it
88  [ qw(c c n m m c) ],                   # pc44
89  [ qw(c c n m) ],                               # pc45
90  [ qw(c c n) ],                                 # pc46
91  undef,                                                 # pc47
92  undef,                                                 # pc48
93  [ qw(c m h) ],                                 # pc49
94  [ qw(c n h) ],                                 # pc50
95  [ qw(c c n) ],                                 # pc51
96  undef,
97  undef,
98  undef,
99  undef,
100  undef,
101  undef,
102  undef,
103  undef,
104  undef,                                                 # pc60
105  undef,
106  undef,
107  undef,
108  undef,
109  undef,
110  undef,
111  undef,
112  undef,
113  undef,
114  undef,                                                 # pc70
115  undef,
116  undef,
117  [ qw(d n n n n n n m m m c c h) ],     # pc73
118  undef,
119  undef,
120  undef,
121  undef,
122  undef,
123  undef,
124  undef,                                                 # pc80
125  undef,
126  undef,
127  undef,
128  [ qw(c c c m) ],                               # pc84
129  [ qw(c c c m) ],                               # pc85
130 );
131
132 # use the entry in the check list to check the field list presented
133 # return OK if line NOT in check list (for now)
134 sub check
135 {
136         my $n = shift;
137         $n -= 10;
138         return 0 if $n < 0 || $n > @checklist; 
139         my $ref = $checklist[$n];
140         return 0 unless ref $ref;
141         
142         my $i;
143         shift;    # not interested in the first field
144         for ($i = 0; $i < @$ref; $i++) {
145                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
146                 return 0 unless $act;
147                 next if $blank && $_[$i] =~ /^[ \*]$/;
148                 if ($act eq 'c') {
149                         return $i+1 unless is_callsign($_[$i]);
150                 } elsif ($act eq 'm') {
151                         return $i+1 unless is_pctext($_[$i]);
152                 } elsif ($act eq 'p') {
153                         return $i+1 unless is_pcflag($_[$i]);
154                 } elsif ($act eq 'f') {
155                         return $i+1 unless is_freq($_[$i]);
156                 } elsif ($act eq 'n') {
157                         return $i+1 unless $_[$i] =~ /^[\d ]+$/;
158                 } elsif ($act eq 'h') {
159                         return $i+1 unless $_[$i] =~ /^H\d\d?$/;
160                 } elsif ($act eq 'd') {
161                         return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
162                 } elsif ($act eq 't') {
163                         return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
164                 }
165         }
166         return 0;
167 }
168
169 sub init
170 {
171         my $user = DXUser->get($main::mycall);
172         $DXProt::myprot_version += $main::version*100;
173         $me = DXProt->new($main::mycall, 0, $user); 
174         $me->{here} = 1;
175         $me->{state} = "indifferent";
176         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
177         confess $@ if $@;
178         $me->{sort} = 'S';    # S for spider
179
180         # now prime the spot and wwv  duplicates file with data
181 #    my @today = Julian::unixtoj(time);
182 #       for (Spot::readfile(@today), Spot::readfile(Julian::sub(@today, 1))) {
183 #               Spot::dup(@{$_}[0..3]);
184 #       }
185 #       for (Geomag::readfile(time)) {
186 #               Geomag::dup(@{$_}[1..5]);
187 #       }
188
189         # load the baddx file
190         do "$baddxfn" if -e "$baddxfn";
191         print "$@\n" if $@;
192 }
193
194 #
195 # obtain a new connection this is derived from dxchannel
196 #
197
198 sub new 
199 {
200         my $self = DXChannel::alloc(@_);
201         return $self;
202 }
203
204 # this is how a pc connection starts (for an incoming connection)
205 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
206 # all the crap that comes between).
207 sub start
208 {
209         my ($self, $line, $sort) = @_;
210         my $call = $self->{call};
211         my $user = $self->{user};
212         
213         # remember type of connection
214         $self->{consort} = $line;
215         $self->{outbound} = $sort eq 'O';
216         $self->{priv} = $user->priv || 1;     # other clusters can always be 'normal' users
217         $self->{lang} = $user->lang || 'en';
218         $self->{isolate} = $user->{isolate};
219         $self->{consort} = $line;       # save the connection type
220         $self->{here} = 1;
221
222         # get the INPUT filters (these only pertain to Clusters)
223         $self->{inspotfilter} = Filter::read_in('spots', $call, 1);
224         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1);
225         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1);
226         $self->{inannfilter} = Filter::read_in('ann', $call, 1);
227         
228         # set unbuffered and no echo
229         $self->send_now('B',"0");
230         $self->send_now('E',"0");
231         
232         # ping neighbour node stuff
233         my $ping = $user->pingint;
234         $ping = 5*60 unless defined $ping;
235         $self->{pingint} = $ping;
236         $self->{nopings} = $user->nopings || 2;
237         $self->{pingtime} = [ ];
238         $self->{pingave} = 0;
239
240         # send initialisation string
241         unless ($self->{outbound}) {
242                 $self->send(pc38()) if DXNode->get_all();
243                 $self->send(pc18());
244                 $self->{lastping} = $main::systime;
245         } else {
246                 # remove from outstanding connects queue
247                 @main::outstanding_connects = grep {$_->{call} ne $call} @main::outstanding_connects;
248                 $self->{lastping} = $main::systime + $self->pingint / 2;
249         }
250         $self->state('init');
251         $self->pc50_t(time);
252
253         # send info to all logged in thingies
254         $self->tell_login('loginn');
255
256         Log('DXProt', "$call connected");
257 }
258
259 #
260 # This is the normal pcxx despatcher
261 #
262 sub normal
263 {
264         my ($self, $line) = @_;
265         my @field = split /\^/, $line;
266         pop @field if $field[-1] eq '~';
267         
268 #       print join(',', @field), "\n";
269                                                 
270         # ignore any lines that don't start with PC
271         return if !$field[0] =~ /^PC/;
272         
273         # process PC frames
274         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
275         return unless $pcno;
276         return if $pcno < 10 || $pcno > 99;
277
278         # check for and dump bad protocol messages
279         my $n = check($pcno, @field);
280         if ($n) {
281                 dbg('chan', "bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
282                 return;
283         }
284
285         # local processing 1
286         my $pcr;
287         eval {
288                 $pcr = Local::pcprot($self, $pcno, @field);
289         };
290 #       dbg('local', "Local::pcprot error $@") if $@;
291         return if $pcr;
292         
293  SWITCH: {
294                 if ($pcno == 10) {              # incoming talk
295                         
296                         # is it for me or one of mine?
297                         my ($to, $via, $call, $dxchan);
298                         if ($field[5] gt ' ') {
299                                 $call = $via = $field[2];
300                                 $to = $field[5];
301                                 unless (is_callsign($to)) {
302                                         dbg('chan', "Corrupt talk, rejected");
303                                         return;
304                                 }
305                         } else {
306                                 $call = $to = $field[2];
307                         }
308                         if ($dxchan = DXChannel->get($call)) {
309                                 $dxchan->talk($field[1], $to, $via, $field[3]);
310                         } else {
311                                 $self->route($field[2], $line); # relay it on its way
312                         }
313                         return;
314                 }
315                 
316                 if ($pcno == 11 || $pcno == 26) { # dx spot
317
318                         # route 'foreign' pc26s 
319                         if ($pcno == 26) {
320                                 if ($field[7] ne $main::mycall) {
321                                         $self->route($field[7], $line);
322                                         return;
323                                 }
324                         }
325                         
326                         # if this is a 'nodx' node then ignore it
327                         if (grep $field[7] =~ /^$_/,  @DXProt::nodx_node) {
328                                 dbg('chan', "Bad DXNode, dropped");
329                                 return;
330                         }
331                         
332                         # convert the date to a unix date
333                         my $d = cltounix($field[3], $field[4]);
334                         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
335                         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
336                                 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
337                                 return;
338                         }
339
340                         # is it 'baddx'
341                         if (grep $field[2] eq $_, @baddx) {
342                                 dbg('chan', "Bad DX spot, ignored");
343                                 return;
344                         }
345                         
346                         # do some de-duping
347                         $field[5] =~ s/^\s+//;      # take any leading blanks off
348                         if (Spot::dup($field[1], $field[2], $d, $field[5])) {
349                                 dbg('chan', "Duplicate Spot ignored\n");
350                                 return;
351                         }
352                         
353                         my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
354
355             #
356                         # @spot at this point contains:-
357             # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
358                         # then  spotted itu, spotted cq, spotters itu, spotters cq
359                         # you should be able to route on any of these
360             #
361                         
362                         # fix up qra locators of known users 
363                         my $user = DXUser->get_current($spot[4]);
364                         if ($user) {
365                                 my $qra = $user->qra;
366                                 unless ($qra && DXBearing::is_qra($qra)) {
367                                         my $lat = $user->lat;
368                                         my $long = $user->long;
369                                         if (defined $lat && defined $long) {
370                                                 $user->qra(DXBearing::lltoqra($lat, $long)); 
371                                                 $user->put;
372                                         }
373                                 }
374
375                                 # send a remote command to a distant cluster if it is visible and there is no
376                                 # qra locator and we havn't done it for a month.
377
378                                 unless ($user->qra) {
379                                         my $node;
380                                         my $to = $user->homenode;
381                                         my $last = $user->lastoper || 0;
382                                         if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = DXCluster->get_exact($to)) ) {
383                                                 my $cmd = "forward/opernam $spot[4]";
384                                                 # send the rcmd but we aren't interested in the replies...
385                                                 if ($node && $node->dxchan && $node->dxchan->is_clx) {
386                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
387                                                 } else {
388                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
389                                                 }
390                                                 if ($to ne $field[7]) {
391                                                         $to = $field[7];
392                                                         $node = DXCluster->get_exact($to);
393                                                         if ($node && $node->dxchan && $node->dxchan->is_clx) {
394                                                                 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
395                                                         } else {
396                                                                 route(undef, $to, pc34($main::mycall, $to, $cmd));
397                                                         }
398                                                 }
399                                                 $user->lastoper($main::systime);
400                                                 $user->put;
401                                         }
402                                 }
403                         }
404                                 
405                         # local processing 
406                         my $r;
407                         eval {
408                                 $r = Local::spot($self, @spot);
409                         };
410 #                       dbg('local', "Local::spot1 error $@") if $@;
411                         return if $r;
412
413                         # DON'T be silly and send on PC26s!
414                         return if $pcno == 26;
415
416                         # send out the filtered spots
417                         send_dx_spot($self, $line, @spot) if @spot;
418                         return;
419                 }
420                 
421                 if ($pcno == 12) {              # announces
422                         # announce duplicate checking
423                         $field[3] =~ s/^\s+//;  # remove leading blanks
424                         if (AnnTalk::dup($field[1], $field[2], $field[3])) {
425                                 dbg('chan', "Duplicate Announce ignored");
426                                 return;
427                         }
428                         
429                         if ($field[2] eq '*' || $field[2] eq $main::mycall) {
430                                 
431                                 # global ann filtering on INPUT
432                                 if ($self->{inannfilter}) {
433                                         my ($filter, $hops) = Filter::it($self->{inannfilter}, @field[1..6], $self->{call} );
434                                         unless ($filter) {
435                                                 dbg('chan', "Rejected by filter");
436                                                 return;
437                                         }
438                                 }
439
440                                 # send it
441                                 $self->send_announce($line, @field[1..6]);
442                         } else {
443                                 $self->route($field[2], $line);
444                         }
445                         
446                         return;
447                 }
448                 
449                 if ($pcno == 13) {
450                         last SWITCH;
451                 }
452                 if ($pcno == 14) {
453                         last SWITCH;
454                 }
455                 if ($pcno == 15) {
456                         last SWITCH;
457                 }
458                 
459                 if ($pcno == 16) {              # add a user
460                         my $node = DXCluster->get_exact($field[1]); 
461                         my $dxchan;
462                         if (!$node && ($dxchan = DXChannel->get($field[1]))) {
463                                 # add it to the node table if it isn't present and it's
464                                 # connected locally
465                                 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
466                                 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
467                                 
468                         }
469                         return unless $node; # ignore if havn't seen a PC19 for this one yet
470                         return unless $node->isa('DXNode');
471                         if ($node->dxchan != $self) {
472                                 dbg('chan', "LOOP: $field[1] came in on wrong channel");
473                                 return;
474                         }
475                         if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
476                                 dbg('chan', "LOOP: $field[1] connected locally");
477                                 return;
478                         }
479                         my $i;
480                                                 
481                         for ($i = 2; $i < $#field; $i++) {
482                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
483                                 next if !$call || length $call < 3 || length $call > 8;
484                                 next if !$confmode;
485                                 $call = uc $call;
486                                 next if DXCluster->get_exact($call); # we already have this (loop?)
487                                 
488                                 $confmode = $confmode eq '*';
489                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
490                                 
491                                 # add this station to the user database, if required
492                                 $call =~ s/-\d+$//o;        # remove ssid for users
493                                 my $user = DXUser->get_current($call);
494                                 $user = DXUser->new($call) if !$user;
495                                 $user->homenode($node->call) if !$user->homenode;
496                                 $user->node($node->call);
497                                 $user->lastin($main::systime) unless DXChannel->get($call);
498                                 $user->put;
499                         }
500                         
501                         # queue up any messages (look for privates only)
502                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
503                         last SWITCH;
504                 }
505                 
506                 if ($pcno == 17) {              # remove a user
507                         my $node = DXCluster->get_exact($field[2]);
508                         my $dxchan;
509                         if (!$node && ($dxchan = DXChannel->get($field[2]))) {
510                                 # add it to the node table if it isn't present and it's
511                                 # connected locally
512                                 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
513                                 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
514                                 return;
515                         }
516                         return unless $node;
517                         return unless $node->isa('DXNode');
518                         if ($node->dxchan != $self) {
519                                 dbg('chan', "LOOP: $field[2] came in on wrong channel");
520                                 return;
521                         }
522                         if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
523                                 dbg('chan', "LOOP: $field[2] connected locally");
524                                 return;
525                         }
526                         my $ref = DXCluster->get_exact($field[1]);
527                         $ref->del() if $ref;
528                         last SWITCH;
529                 }
530                 
531                 if ($pcno == 18) {              # link request
532                         $self->state('init');   
533
534                         # first clear out any nodes on this dxchannel
535                         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
536                         foreach my $node (@gonenodes) {
537                                 next if $node->dxchan == $DXProt::me;
538                                 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate}; 
539                                 $node->del();
540                         }
541                         $self->send_local_config();
542                         $self->send(pc20());
543                         return;             # we don't pass these on
544                 }
545                 
546                 if ($pcno == 19) {              # incoming cluster list
547                         my $i;
548                         my $newline = "PC19^";
549                         for ($i = 1; $i < $#field-1; $i += 4) {
550                                 my $here = $field[$i];
551                                 my $call = uc $field[$i+1];
552                                 my $confmode = $field[$i+2];
553                                 my $ver = $field[$i+3];
554
555                                 $ver = 5400 if !$ver && $allowzero;
556                                 
557                                 # now check the call over
558                                 my $node = DXCluster->get_exact($call);
559                                 if ($node) {
560                                         my $dxchan;
561                                         if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
562                                                 dbg('chan', "LOOP: $call connected locally");
563                                         }
564                                     if ($node->dxchan != $self) {
565                                                 dbg('chan', "LOOP: $call come in on wrong channel");
566                                                 next;
567                                         }
568                                         dbg('chan', "already have $call");
569                                         next;
570                                 }
571                                 
572                                 # check for sane parameters
573                                 next if $ver < 5000; # only works with version 5 software
574                                 next if length $call < 3; # min 3 letter callsigns
575
576                                 # add it to the nodes table and outgoing line
577                                 $newline .= "$here^$call^$confmode^$ver^";
578                                 DXNode->new($self, $call, $confmode, $here, $ver);
579                                 
580                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
581                                 my $mref = DXMsg::get_busy($call);
582                                 $mref->stop_msg($call) if $mref;
583                                 
584                                 # add this station to the user database, if required (don't remove SSID from nodes)
585                                 my $user = DXUser->get_current($call);
586                                 if (!$user) {
587                                         $user = DXUser->new($call);
588                                         $user->sort('A');
589                                         $user->priv(1);                   # I have relented and defaulted nodes
590                                         $self->{priv} = 1;                # to user RCMDs allowed
591                                         $user->homenode($call);
592                                         $user->node($call);
593                                 }
594                                 $user->lastin($main::systime) unless DXChannel->get($call);
595                                 $user->put;
596                         }
597                         
598                         return if $newline eq "PC19^";
599
600                         # add hop count 
601                         $newline .=  get_hops(19) . "^";
602                         $line = $newline;
603                         last SWITCH;
604                 }
605                 
606                 if ($pcno == 20) {              # send local configuration
607                         $self->send_local_config();
608                         $self->send(pc22());
609                         $self->state('normal');
610                         return;
611                 }
612                 
613                 if ($pcno == 21) {              # delete a cluster from the list
614                         my $call = uc $field[1];
615                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
616                                 my $node = DXCluster->get_exact($call);
617                                 if ($node) {
618                                         if ($node->dxchan != $self) {
619                                                 dbg('chan', "LOOP: $call come in on wrong channel");
620                                                 return;
621                                         }
622                                         my $dxchan;
623                                         if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
624                                                 dbg('chan', "LOOP: $call connected locally");
625                                                 return;
626                                         }
627                                         $node->del();
628                                 } else {
629                                         dbg('chan', "$call not in table, dropped");
630                                         return;
631                                 }
632                         }
633                         last SWITCH;
634                 }
635                 
636                 if ($pcno == 22) {
637                         $self->state('normal');
638                         return;
639                 }
640                                 
641                 if ($pcno == 23 || $pcno == 27) { # WWV info
642                         
643                         # route 'foreign' pc27s 
644                         if ($pcno == 27) {
645                                 if ($field[8] ne $main::mycall) {
646                                         $self->route($field[8], $line);
647                                         return;
648                                 }
649                         }
650
651                         # do some de-duping
652                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
653                         my $sfi = unpad($field[3]);
654                         my $k = unpad($field[4]);
655                         my $i = unpad($field[5]);
656                         my ($r) = $field[6] =~ /R=(\d+)/;
657                         $r = 0 unless $r;
658                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
659                                 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
660                                 return;
661                         }
662                         if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
663                                 dbg('chan', "Dup WWV Spot ignored\n");
664                                 return;
665                         }
666                         $field[7] =~ s/-\d+$//o;            # remove spotter's ssid
667                 
668                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
669
670                         my $rep;
671                         eval {
672                                 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
673                         };
674 #                       dbg('local', "Local::wwv2 error $@") if $@;
675                         return if $rep;
676
677                         # DON'T be silly and send on PC27s!
678                         return if $pcno == 27;
679
680                         # broadcast to the eager world
681                         send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
682                         return;
683                 }
684                 
685                 if ($pcno == 24) {              # set here status
686                         my $call = uc $field[1];
687                         my $ref = DXCluster->get_exact($call);
688                         $ref->here($field[2]) if $ref;
689                         last SWITCH;
690                 }
691                 
692                 if ($pcno == 25) {      # merge request
693                         if ($field[1] ne $main::mycall) {
694                                 $self->route($field[1], $line);
695                                 return;
696                         }
697                         if ($field[2] eq $main::mycall) {
698                                 dbg('chan', "Trying to merge to myself, ignored");
699                                 return;
700                         }
701
702                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
703                         
704                         # spots
705                         if ($field[3] > 0) {
706                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
707                                 my $in;
708                                 foreach $in (@in) {
709                                         $self->send(pc26(@{$in}[0..4], $field[2]));
710                                 }
711                         }
712
713                         # wwv
714                         if ($field[4] > 0) {
715                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
716                                 my $in;
717                                 foreach $in (@in) {
718                                         $self->send(pc27(@{$in}[0..5], $field[2]));
719                                 }
720                         }
721                         return;
722                 }
723
724                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
725                         if ($pcno == 49 || $field[1] eq $main::mycall) {
726                                 DXMsg::process($self, $line);
727                         } else {
728                                 $self->route($field[1], $line) unless $self->is_clx;
729                         }
730                         return;
731                 }
732                 
733                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
734                         if ($field[1] eq $main::mycall) {
735                                 my $ref = DXUser->get_current($field[2]);
736                                 my $cref = DXCluster->get($field[2]);
737                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
738                                 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
739                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
740                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
741                                                 my $oldpriv = $self->{priv};
742                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
743                                                 my @in = (DXCommandmode::run_cmd($self, $field[3]));
744                                                 $self->{priv} = $oldpriv;
745                                                 for (@in) {
746                                                         s/\s*$//og;
747                                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
748                                                         Log('rcmd', 'out', $field[2], $_);
749                                                 }
750                                                 delete $self->{remotecmd};
751                                         } else {
752                                                 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
753                                         }
754                                 } else {
755                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
756                                 }
757                         } else {
758                                 my $ref = DXUser->get_current($field[1]);
759                                 if ($ref && $ref->is_clx) {
760                                         route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
761                                 } else {
762                                         $self->route($field[1], $line);
763                                 }
764                         }
765                         return;
766                 }
767                 
768                 if ($pcno == 35) {              # remote command replies
769                         if ($field[1] eq $main::mycall) {
770                                 my $s = $rcmds{$field[2]};
771                                 if ($s) {
772                                         my $dxchan = DXChannel->get($s->{call});
773                                         $dxchan->send($field[3]) if $dxchan;
774                                         delete $rcmds{$field[2]} if !$dxchan;
775                                 } else {
776                                         # send unsolicited ones to the sysop
777                                         my $dxchan = DXChannel->get($main::myalias);
778                                         $dxchan->send($field[3]) if $dxchan;
779                                 }
780                         } else {
781                                 my $ref = DXUser->get_current($field[1]);
782                                 if ($ref && $ref->is_clx) {
783                                         route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
784                                 } else {
785                                         $self->route($field[1], $line);
786                                 }
787                         }
788                         return;
789                 }
790                 
791                 # for pc 37 see 44 onwards
792
793                 if ($pcno == 38) {              # node connected list from neighbour
794                         return;
795                 }
796                 
797                 if ($pcno == 39) {              # incoming disconnect
798                         $self->disconnect(1);
799                         return;
800                 }
801                 
802                 if ($pcno == 41) {              # user info
803                         # add this station to the user database, if required
804                         my $user = DXUser->get_current($field[1]);
805                         if (!$user) {
806                                 # then try without an SSID
807                                 $field[1] =~ s/-\d+$//o;
808                                 $user = DXUser->get_current($field[1]);
809                         }
810                         $user = DXUser->new($field[1]) if !$user;
811                         
812                         if ($field[2] == 1) {
813                                 $user->name($field[3]);
814                         } elsif ($field[2] == 2) {
815                                 $user->qth($field[3]);
816                         } elsif ($field[2] == 3) {
817                                 my ($lat, $long) = DXBearing::stoll($field[3]);
818                                 $user->lat($lat);
819                                 $user->long($long);
820                                 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
821                         } elsif ($field[2] == 4) {
822                                 $user->homenode($field[3]);
823                         }
824                         $user->lastoper($main::systime);   # to cut down on excessive for/opers being generated
825                         $user->put;
826                         last SWITCH;
827                 }
828                 if ($pcno == 43) {
829                         last SWITCH;
830                 }
831                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
832                         DXDb::process($self, $line);
833                         return;
834                 }
835                 
836                 if ($pcno == 50) {              # keep alive/user list
837                         my $node = DXCluster->get_exact($field[1]);
838                         if ($node) {
839                                 return unless $node->isa('DXNode');
840                                 return unless $node->dxchan == $self;
841                                 $node->update_users($field[2]);
842                         }
843                         last SWITCH;
844                 }
845                 
846                 if ($pcno == 51) {              # incoming ping requests/answers
847                         
848                         # is it for us?
849                         if ($field[1] eq $main::mycall) {
850                                 my $flag = $field[3];
851                                 if ($flag == 1) {
852                                         $self->send(pc51($field[2], $field[1], '0'));
853                                 } else {
854                                         # it's a reply, look in the ping list for this one
855                                         my $ref = $pings{$field[2]};
856                                         if ($ref) {
857                                                 my $tochan =  DXChannel->get($field[2]);
858                                                 while (@$ref) {
859                                                         my $r = shift @$ref;
860                                                         my $dxchan = DXChannel->get($r->{call});
861                                                         next unless $dxchan;
862                                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
863                                                         if ($dxchan->is_user) {
864                                                                 my $s = sprintf "%.2f", $t; 
865                                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
866                                                                 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
867                                                         } elsif ($dxchan->is_node) {
868                                                                 if ($tochan) {
869                                                                         $tochan->{nopings} = 2; # pump up the timer
870                                                                         push @{$tochan->{pingtime}}, $t;
871                                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
872                                                                         my $st;
873                                                                         for (@{$tochan->{pingtime}}) {
874                                                                                 $st += $_;
875                                                                         }
876                                                                         $tochan->{pingave} = $st / @{$tochan->{pingtime}};
877                                                                 }
878                                                         } 
879                                                 }
880                                         }
881                                 }
882                         } else {
883                                 # route down an appropriate thingy
884                                 $self->route($field[1], $line);
885                         }
886                         return;
887                 }
888
889                 if ($pcno == 75) {              # dunno but route it
890                         if ($field[1] ne $main::mycall) {
891                                 $self->route($field[1], $line);
892                         }
893                         return;
894                 }
895
896                 if ($pcno == 73) {  # WCY broadcasts
897                         
898                         # do some de-duping
899                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
900                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
901                                 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
902                                 return;
903                         }
904                         @field = map { unpad($_) } @field;
905                         if (WCY::dup($d,@field[3..7])) {
906                                 dbg('chan', "Dup WCY Spot ignored\n");
907                                 return;
908                         }
909                 
910                         my $wcy = WCY::update($d, @field[2..12]);
911
912                         my $rep;
913                         eval {
914                                 $rep = Local::wwv($self, @field[1..12]);
915                         };
916                         # dbg('local', "Local::wcy error $@") if $@;
917                         return if $rep;
918
919                         # broadcast to the eager world
920                         send_wcy_spot($self, $line, $d, @field[2..12]);
921                         return;
922                 }
923
924                 if ($pcno == 84) { # remote commands (incoming)
925                         if ($field[1] eq $main::mycall) {
926                                 my $ref = DXUser->get_current($field[2]);
927                                 my $cref = DXCluster->get($field[2]);
928                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
929                                 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
930                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
931                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
932                                                 my $oldpriv = $self->{priv};
933                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
934                                                 my @in = (DXCommandmode::run_cmd($self, $field[4]));
935                                                 $self->{priv} = $oldpriv;
936                                                 for (@in) {
937                                                         s/\s*$//og;
938                                                         $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
939                                                         Log('rcmd', 'out', $field[2], $_);
940                                                 }
941                                                 delete $self->{remotecmd};
942                                         } else {
943                                                 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
944                                         }
945                                 } else {
946                                         $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
947                                 }
948                         } else {
949                                 my $ref = DXUser->get_current($field[1]);
950                                 if ($ref && $ref->is_clx) {
951                                         $self->route($field[1], $line);
952                                 } else {
953                                         route($field[1], pc34($field[2], $field[1], $field[4]));
954                                 }
955                         }
956                         return;
957                 }
958
959                 if ($pcno == 85) {              # remote command replies
960                         if ($field[1] eq $main::mycall) {
961                                 my $dxchan = DXChannel->get($field[3]);
962                                 if ($dxchan) {
963                                         $dxchan->send($field[4]);
964                                 } else {
965                                         my $s = $rcmds{$field[2]};
966                                         if ($s) {
967                                                 $dxchan = DXChannel->get($s->{call});
968                                                 $dxchan->send($field[4]) if $dxchan;
969                                                 delete $rcmds{$field[2]} if !$dxchan;
970                                         } else {
971                                                 # send unsolicited ones to the sysop
972                                                 my $dxchan = DXChannel->get($main::myalias);
973                                                 $dxchan->send($field[4]) if $dxchan;
974                                         }
975                                 }
976                         } else {
977                                 my $ref = DXUser->get_current($field[1]);
978                                 if ($ref && $ref->is_clx) {
979                                         $self->route($field[1], $line);
980                                 } else {
981                                         route($field[1], pc35($field[2], $field[1], $field[4]));
982                                 }
983                         }
984                         return;
985                 }
986         }
987          
988         # if get here then rebroadcast the thing with its Hop count decremented (if
989         # there is one). If it has a hop count and it decrements to zero then don't
990         # rebroadcast it.
991         #
992         # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
993         #        REBROADCAST!!!!
994         #
995          
996         unless ($self->{isolate}) {
997                 broadcast_ak1a($line, $self); # send it to everyone but me
998         }
999 }
1000
1001 #
1002 # This is called from inside the main cluster processing loop and is used
1003 # for despatching commands that are doing some long processing job
1004 #
1005 sub process
1006 {
1007         my $t = time;
1008         my @dxchan = DXChannel->get_all();
1009         my $dxchan;
1010         
1011         foreach $dxchan (@dxchan) {
1012                 next unless $dxchan->is_node();
1013                 next if $dxchan == $me;
1014                 
1015                 # send a pc50 out on this channel
1016                 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
1017                         $dxchan->send(pc50(scalar DXChannel::get_all_users));
1018                         $dxchan->pc50_t($t);
1019                 } 
1020
1021                 # send a ping out on this channel
1022                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1023                         if ($dxchan->{nopings} <= 0) {
1024                                 $dxchan->disconnect;
1025                         } else {
1026                                 addping($main::mycall, $dxchan->call);
1027                                 $dxchan->{nopings} -= 1;
1028                                 $dxchan->{lastping} = $t;
1029                         }
1030                 }
1031         }
1032         
1033         my $key;
1034         my $val;
1035         my $cutoff;
1036         if ($main::systime - 3600 > $last_hour) {
1037 #               Spot::process;
1038 #               Geomag::process;
1039 #               AnnTalk::process;
1040                 $last_hour = $main::systime;
1041         }
1042 }
1043
1044 #
1045 # finish up a pc context
1046 #
1047 sub finish
1048 {
1049         my $self = shift;
1050         my $call = $self->call;
1051         my $conn = shift;
1052         my $ref = DXCluster->get_exact($call);
1053         
1054         # unbusy and stop and outgoing mail
1055         my $mref = DXMsg::get_busy($call);
1056         $mref->stop_msg($call) if $mref;
1057         
1058         # broadcast to all other nodes that all the nodes connected to via me are gone
1059         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
1060         my $node;
1061         
1062         foreach $node (@gonenodes) {
1063                 next if $node->call eq $call;
1064                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
1065                 $node->del();
1066         }
1067
1068         # remove outstanding pings
1069         delete $pings{$call};
1070         
1071         # now broadcast to all other ak1a nodes that I have gone
1072         broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
1073
1074         # I was the last node visited
1075     $self->user->node($main::mycall);
1076
1077         # send info to all logged in thingies
1078         $self->tell_login('logoutn');
1079
1080         Log('DXProt', $call . " Disconnected");
1081         $ref->del() if $ref;
1082 }
1083
1084 #
1085 # some active measures
1086 #
1087 sub send_dx_spot
1088 {
1089         my $self = shift;
1090         my $line = shift;
1091         my @dxchan = DXChannel->get_all();
1092         my $dxchan;
1093         
1094         # send it if it isn't the except list and isn't isolated and still has a hop count
1095         # taking into account filtering and so on
1096         foreach $dxchan (@dxchan) {
1097                 my $routeit;
1098                 my ($filter, $hops);
1099
1100                 if ($dxchan->{spotfilter}) {
1101                     ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
1102                         next unless $filter;
1103                 }
1104                 
1105                 if ($dxchan->is_node) {
1106                         next if $dxchan == $self;
1107                         if ($hops) {
1108                                 $routeit = $line;
1109                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1110                         } else {
1111                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1112                                 next unless $routeit;
1113                         }
1114                         if ($filter) {
1115                                 $dxchan->send($routeit) if $routeit;
1116                         } else {
1117                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1118                         }
1119                 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1120                         my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1121                         $buf .= "\a\a" if $dxchan->{beep};
1122                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1123                                 $dxchan->send($buf);
1124                         } else {
1125                                 $dxchan->delay($buf);
1126                         }
1127                 }                                       
1128         }
1129 }
1130
1131 sub send_wwv_spot
1132 {
1133         my $self = shift;
1134         my $line = shift;
1135         my @dxchan = DXChannel->get_all();
1136         my $dxchan;
1137         
1138         # send it if it isn't the except list and isn't isolated and still has a hop count
1139         # taking into account filtering and so on
1140         foreach $dxchan (@dxchan) {
1141                 my $routeit;
1142                 my ($filter, $hops);
1143
1144                 if ($dxchan->{wwvfilter}) {
1145                          ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
1146                          next unless $filter;
1147                 }
1148                 if ($dxchan->is_node) {
1149                         next if $dxchan == $self;
1150                         if ($hops) {
1151                                 $routeit = $line;
1152                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1153                         } else {
1154                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1155                                 next unless $routeit;
1156                         }
1157                         if ($filter) {
1158                                 $dxchan->send($routeit) if $routeit;
1159                         } else {
1160                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1161                                 
1162                         }
1163                 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1164                         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1165                         $buf .= "\a\a" if $dxchan->{beep};
1166                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1167                                 $dxchan->send($buf);
1168                         } else {
1169                                 $dxchan->delay($buf);
1170                         }
1171                 }                                       
1172         }
1173 }
1174
1175 sub send_wcy_spot
1176 {
1177         my $self = shift;
1178         my $line = shift;
1179         my @dxchan = DXChannel->get_all();
1180         my $dxchan;
1181         
1182         # send it if it isn't the except list and isn't isolated and still has a hop count
1183         # taking into account filtering and so on
1184         foreach $dxchan (@dxchan) {
1185                 my $routeit;
1186                 my ($filter, $hops);
1187
1188                 if ($dxchan->{wcyfilter}) {
1189                          ($filter, $hops) = Filter::it($dxchan->{wcyfilter}, @_, $self->{call} );
1190                          next unless $filter;
1191                 }
1192                 if ($dxchan->is_clx || $dxchan->is_spider) {
1193                         next if $dxchan == $self;
1194                         if ($hops) {
1195                                 $routeit = $line;
1196                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1197                         } else {
1198                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1199                                 next unless $routeit;
1200                         }
1201                         if ($filter) {
1202                                 $dxchan->send($routeit) if $routeit;
1203                         } else {
1204                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1205                         }
1206                 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1207                         my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1208                         $buf .= "\a\a" if $dxchan->{beep};
1209                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1210                                 $dxchan->send($buf);
1211                         } else {
1212                                 $dxchan->delay($buf);
1213                         }
1214                 }                                       
1215         }
1216 }
1217
1218 # send an announce
1219 sub send_announce
1220 {
1221         my $self = shift;
1222         my $line = shift;
1223         my @dxchan = DXChannel->get_all();
1224         my $dxchan;
1225         my $text = unpad($_[2]);
1226         my $target;
1227         my $to = 'To ';
1228                                 
1229         if ($_[3] eq '*') {     # sysops
1230                 $target = "SYSOP";
1231         } elsif ($_[3] gt ' ') { # speciality list handling
1232                 my ($name) = split /\./, $_[3]; 
1233                 $target = "$name"; # put the rest in later (if bothered) 
1234         } 
1235         
1236         if ($_[5] eq '1') {
1237                 $target = "WX"; 
1238                 $to = '';
1239         }
1240         $target = "All" if !$target;
1241         
1242         Log('ann', $target, $_[0], $text);
1243
1244         # send it if it isn't the except list and isn't isolated and still has a hop count
1245         # taking into account filtering and so on
1246         foreach $dxchan (@dxchan) {
1247                 my $routeit;
1248                 my ($filter, $hops);
1249
1250                 if ($dxchan->{annfilter}) {
1251                         ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
1252                         next unless $filter;
1253                 } 
1254                 if ($dxchan->is_node && $_[1] ne $main::mycall) {  # i.e not specifically routed to me
1255                         next if $dxchan == $self;
1256                         if ($hops) {
1257                                 $routeit = $line;
1258                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1259                         } else {
1260                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1261                                 next unless $routeit;
1262                         }
1263                         if ($filter) {
1264                                 $dxchan->send($routeit) if $routeit;
1265                         } else {
1266                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1267                                 
1268                         }
1269                 } elsif ($dxchan->is_user) {
1270                         unless ($dxchan->{ann}) {
1271                                 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1272                         }
1273                         next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1274                         my $buf = "$to$target de $_[0]: $text";
1275                         $buf .= "\a\a" if $dxchan->{beep};
1276                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1277                                 $dxchan->send($buf);
1278                         } else {
1279                                 $dxchan->delay($buf);
1280                         }
1281                 }                                       
1282         }
1283 }
1284
1285 sub send_local_config
1286 {
1287         my $self = shift;
1288         my $n;
1289         my @nodes;
1290         my @localnodes;
1291         my @remotenodes;
1292                 
1293         # send our nodes
1294         if ($self->{isolate}) {
1295                 @localnodes = (DXCluster->get_exact($main::mycall));
1296         } else {
1297                 # create a list of all the nodes that are not connected to this connection
1298                 # and are not themselves isolated, this to make sure that isolated nodes
1299         # don't appear outside of this node
1300                 @nodes = DXNode::get_all();
1301                 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1302                 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1303                 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1304                 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1305                 unshift @localnodes, DXCluster->get_exact($main::mycall);
1306                 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1307         }
1308
1309         my @s = $me->pc19(@localnodes, @remotenodes);
1310         for (@s) {
1311                 my $routeit = adjust_hops($self, $_);
1312                 $self->send($routeit) if $routeit;
1313         }
1314         
1315         # get all the users connected on the above nodes and send them out
1316         foreach $n (@localnodes, @remotenodes) {
1317                 my @users = values %{$n->list};
1318                 my @s = pc16($n, @users);
1319                 for (@s) {
1320                         my $routeit = adjust_hops($self, $_);
1321                         $self->send($routeit) if $routeit;
1322                 }
1323         }
1324 }
1325
1326 #
1327 # route a message down an appropriate interface for a callsign
1328 #
1329 # is called route(to, pcline);
1330 #
1331 sub route
1332 {
1333         my ($self, $call, $line) = @_;
1334         my $cl = DXCluster->get_exact($call);
1335         if ($cl) {       # don't route it back down itself
1336                 if (ref $self && $call eq $self->{call}) {
1337                         dbg('chan', "Trying to route back to source, dropped");
1338                         return;
1339                 }
1340                 my $hops;
1341                 my $dxchan = $cl->{dxchan};
1342                 if ($dxchan) {
1343                         my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1344                         if ($routeit) {
1345                                 $dxchan->send($routeit) if $dxchan;
1346                         }
1347                 }
1348         }
1349 }
1350
1351 # broadcast a message to all clusters taking into account isolation
1352 # [except those mentioned after buffer]
1353 sub broadcast_ak1a
1354 {
1355         my $s = shift;                          # the line to be rebroadcast
1356         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1357         my @dxchan = DXChannel::get_all_ak1a();
1358         my $dxchan;
1359         
1360         # send it if it isn't the except list and isn't isolated and still has a hop count
1361         foreach $dxchan (@dxchan) {
1362                 next if grep $dxchan == $_, @except;
1363                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1364                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1365         }
1366 }
1367
1368 # broadcast a message to all clusters ignoring isolation
1369 # [except those mentioned after buffer]
1370 sub broadcast_all_ak1a
1371 {
1372         my $s = shift;                          # the line to be rebroadcast
1373         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1374         my @dxchan = DXChannel::get_all_ak1a();
1375         my $dxchan;
1376         
1377         # send it if it isn't the except list and isn't isolated and still has a hop count
1378         foreach $dxchan (@dxchan) {
1379                 next if grep $dxchan == $_, @except;
1380                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1381                 $dxchan->send($routeit);
1382         }
1383 }
1384
1385 # broadcast to all users
1386 # storing the spot or whatever until it is in a state to receive it
1387 sub broadcast_users
1388 {
1389         my $s = shift;                          # the line to be rebroadcast
1390         my $sort = shift;           # the type of transmission
1391         my $fref = shift;           # a reference to an object to filter on
1392         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1393         my @dxchan = DXChannel::get_all_users();
1394         my $dxchan;
1395         my @out;
1396         
1397         foreach $dxchan (@dxchan) {
1398                 next if grep $dxchan == $_, @except;
1399                 push @out, $dxchan;
1400         }
1401         broadcast_list($s, $sort, $fref, @out);
1402 }
1403
1404 # broadcast to a list of users
1405 sub broadcast_list
1406 {
1407         my $s = shift;
1408         my $sort = shift;
1409         my $fref = shift;
1410         my $dxchan;
1411         
1412         foreach $dxchan (@_) {
1413                 my $filter = 1;
1414                 
1415                 if ($sort eq 'dx') {
1416                     next unless $dxchan->{dx};
1417                         ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1418                         next unless $filter;
1419                 }
1420                 next if $sort eq 'ann' && !$dxchan->{ann};
1421                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1422                 next if $sort eq 'wcy' && !$dxchan->{wcy};
1423                 next if $sort eq 'wx' && !$dxchan->{wx};
1424
1425                 $s =~ s/\a//og unless $dxchan->{beep};
1426
1427                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1428                         $dxchan->send($s);      
1429                 } else {
1430                         $dxchan->delay($s);
1431                 }
1432         }
1433 }
1434
1435
1436 #
1437 # obtain the hops from the list for this callsign and pc no 
1438 #
1439
1440 sub get_hops
1441 {
1442         my $pcno = shift;
1443         my $hops = $DXProt::hopcount{$pcno};
1444         $hops = $DXProt::def_hopcount if !$hops;
1445         return "H$hops";       
1446 }
1447
1448
1449 # adjust the hop count on a per node basis using the user loadable 
1450 # hop table if available or else decrement an existing one
1451 #
1452
1453 sub adjust_hops
1454 {
1455         my $self = shift;
1456         my $s = shift;
1457         my $call = $self->{call};
1458         my $hops;
1459         
1460         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1461                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1462                 confess "$call called adjust_hops with '$s'" unless $pcno;
1463                 my $ref = $nodehops{$call} if %nodehops;
1464                 if ($ref) {
1465                         my $newhops = $ref->{$pcno};
1466                         return "" if defined $newhops && $newhops == 0;
1467                         $newhops = $ref->{default} unless $newhops;
1468                         return "" if defined $newhops && $newhops == 0;
1469                         $newhops = $hops if !$newhops;
1470                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1471                 } else {
1472                         # simply decrement it
1473                         $hops--;
1474                         return "" if !$hops;
1475                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1476                 }
1477         }
1478         return $s;
1479 }
1480
1481
1482 # load hop tables
1483 #
1484 sub load_hops
1485 {
1486         my $self = shift;
1487         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1488         do "$main::data/hop_table.pl";
1489         return $@ if $@;
1490         return 0;
1491 }
1492
1493
1494 # add a ping request to the ping queues
1495 sub addping
1496 {
1497         my ($from, $to) = @_;
1498         my $ref = $pings{$to} || [];
1499         my $r = {};
1500         $r->{call} = $from;
1501         $r->{t} = [ gettimeofday ];
1502         route(undef, $to, pc51($to, $main::mycall, 1));
1503         push @$ref, $r;
1504         $pings{$to} = $ref;
1505 }
1506
1507 # add a rcmd request to the rcmd queues
1508 sub addrcmd
1509 {
1510         my ($self, $to, $cmd) = @_;
1511
1512         my $r = {};
1513         $r->{call} = $self->{call};
1514         $r->{t} = $main::systime;
1515         $r->{cmd} = $cmd;
1516         $rcmds{$to} = $r;
1517
1518         my $ref = DXCluster->get_exact($to);
1519     if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1520                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1521         } else {
1522                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1523         }
1524 }
1525
1526 sub disconnect
1527 {
1528         my $self = shift;
1529         my $nopc39 = shift;
1530
1531         if ($self->{conn} && !$nopc39) {
1532                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1533         }
1534
1535         $self->SUPER::disconnect;
1536 }
1537
1538
1539
1540 # send a talk message to this thingy
1541 #
1542 sub talk
1543 {
1544         my ($self, $from, $to, $via, $line) = @_;
1545         
1546         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1547         $self->send(DXProt::pc10($from, $to, $via, $line));
1548         Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);
1549 }
1550 1;
1551 __END__