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