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