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