4 # Copyright (c) - Dirk Koopman G1TLH
22 use vars qw($VERSION $BRANCH);
23 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
24 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
25 $main::build += $VERSION;
26 $main::branch += $BRANCH;
28 use vars qw($db %prefix_loc %pre $lru $lrusize $misses $hits $matchtotal);
30 $db = undef; # the DB_File handle
31 %prefix_loc = (); # the meat of the info
32 %pre = (); # the prefix list
33 $hits = $misses = $matchtotal = 1; # cache stats
34 $lrusize = 1000; # size of prefix LRU cache
41 # fix up the node's default country codes
42 unless (@main::my_cc) {
43 push @main::my_cc, (61..67) if $main::mycall =~ /^GB/;
44 push @main::my_cc, qw(EA EA6 EA8 EA9) if $main::mycall =~ /^E[ABCD]/;
45 push @main::my_cc, qw(I IT IS) if $main::mycall =~ /^I/;
46 push @main::my_cc, qw(SV SV5 SV9) if $main::mycall =~ /^SV/;
49 push @main::my_cc, $main::mycall unless @main::my_cc;
57 my @dxcc = extract($_);
58 push @c, $dxcc[1]->dxcc if @dxcc > 1;
61 return "\@main::my_cc does not contain a valid prefix or callsign (" . join(',', @main::my_cc) . ")" unless @c;
78 # tie the main prefix database
79 eval {$db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0664, $DB_BTREE);};
80 my $out = "$@($!)" if !$db || $@ ;
81 eval {do "$main::data/prefix_data.pl" if !$out; };
83 $lru = LRU->newbase('Prefix', $lrusize);
96 my $fh = new IO::File;
97 my $fn = "$main::data/prefix_data.pl";
99 confess "Prefix system not started" if !$db;
102 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
103 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
104 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
105 rename "$fn.o", "$fn.oo" if -e "$fn.o";
106 rename "$fn", "$fn.o" if -e "$fn";
108 $fh->open(">$fn") or die "Can't open $fn ($!)";
110 # prefix location data
111 $fh->print("%prefix_loc = (\n");
112 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
113 my $r = $prefix_loc{$l};
114 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
115 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
117 $fh->print(");\n\n");
120 $fh->print("%pre = (\n");
121 foreach $k (sort keys %pre) {
122 $fh->print(" '$k' => [");
123 my @list = @{$pre{$k}};
130 $fh->print("$str ],\n");
137 # what you get is a list that looks like:-
139 # prefix => @list of blessed references to prefix_locs
141 # This routine will only do what you ask for, if you wish to be intelligent
142 # then that is YOUR problem!
150 return () if $db->seq($gotkey, $ref, R_CURSOR);
151 return () if $key ne substr $gotkey, 0, length $key;
153 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
157 # get the next key that matches, this assumes that you have done a 'get' first
166 return () if $db->seq($gotkey, $ref, R_NEXT);
167 return () if $key ne substr $gotkey, 0, length $key;
169 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
173 # put the key LRU incluing the city state info
178 my ($call, $ref) = @_;
179 my @s = USDB::get($call);
182 # this is deep magic, because this is a reference to static data, it
184 my $h = { %{$ref->[1]} };
185 bless $h, ref $ref->[1];
190 $ref->[1]->{city} = $ref->[1]->{state} = "" unless exists $ref->[1]->{state};
193 dbg("Prefix::lru_put $call -> ($ref->[1]->{city}, $ref->[1]->{state})") if isdbg('prefix');
194 $lru->put($call, $ref);
198 # search for the nearest match of a prefix string (starting
199 # from the RH end of the string passed)
207 for (my $i = length $pref; $i; $i--) {
209 my $s = substr($pref, 0, $i);
211 my $p = $lru->get($s);
214 if (isdbg('prefix')) {
215 my $percent = sprintf "%.1f", $hits * 100 / $misses;
216 dbg("Partial Prefix Cache Hit: $s Hits: $hits/$misses of $matchtotal = $percent\%");
218 lru_put($_, $p) for @partials;
223 if (isdbg('prefix')) {
224 my $part = $out[0] || "*";
225 $part .= '*' unless $part eq '*' || $part eq $s;
226 dbg("Partial prefix: $pref $s $part" );
228 if (@out && $out[0] eq $s) {
237 # extract a 'prefix' from a callsign, in other words the largest entity that will
238 # obtain a result from the prefix table.
240 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
241 # G1TLH/VO1 (should) return VO1
246 my $calls = uc shift;
252 LM: foreach $call (split /,/, $calls) {
254 # first check if the whole thing succeeds either because it is cached
255 # or because it simply is a stored prefix as callsign (or even a prefix)
257 $call =~ s/-\d+$//; # ignore SSIDs
258 my $p = $lru->get($call);
262 if (isdbg('prefix')) {
263 my $percent = sprintf "%.1f", $hits * 100 / $misses;
264 dbg("Prefix Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
270 # is it in the USDB, force a matchprefix to match?
271 my @s = USDB::get($call);
274 @nout = matchprefix($call) unless @nout;
275 $nout[0] = $call if @nout;
281 if (@nout && $nout[0] eq $call) {
283 lru_put($call, \@nout);
284 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
290 # now split the call into parts if required
291 @parts = ($call =~ '/') ? split('/', $call) : ($call);
292 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
294 # remove any /0-9 /P /A /M /MM /AM suffixes etc
296 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
298 # can we resolve them by direct lookup
299 my $s = join('/', @parts);
301 if (@nout && $nout[0] eq $s) {
302 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
304 lru_put($call, \@nout);
309 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
311 # at this point we should have two or three parts
312 # if it is three parts then join the first and last parts together
315 # first deal with prefix/x00xx/single letter things
316 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
317 @nout = matchprefix($parts[0]);
319 my $s = join('/', $nout[0], $parts[2]);
321 if (@try && $try[0] eq $s) {
322 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
324 lru_put($call, \@try);
329 # if the second part is a callsign and the last part is one letter
330 if (is_callsign($parts[1]) && length $parts[2] == 1) {
336 # if it is a two parter
339 # try it as it is as compound, taking the first part as the prefix
340 @nout = matchprefix($parts[0]);
342 my $s = join('/', $nout[0], $parts[1]);
344 if (@try && $try[0] eq $s) {
345 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
347 lru_put($call, \@try);
354 # remove the problematic /J suffix
355 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
359 @nout = matchprefix($parts[0]);
361 dbg("got prefix: $call = $nout[0]") if isdbg('prefix');
363 lru_put($call, \@nout);
372 L1: for ($n = 0; $n < @parts; $n++) {
375 for ($i = $k = 0; $i < @parts; $i++) {
376 next if $checked[$i];
378 if (!$sp || length $p < length $sp) {
379 dbg("try part: $p") if isdbg('prefix');
385 $sp =~ s/-\d+$//; # remove any SSID
387 # now start to resolve it from the right hand end
388 @nout = matchprefix($sp);
390 # try and search for it in the descriptions as
391 # a whole callsign if it has multiple parts and the output
392 # is more two long, this should catch things like
393 # FR5DX/T without having to explicitly stick it into
398 $parts[$k] = $nout[0];
399 my $try = join('/', @parts);
401 if (isdbg('prefix')) {
402 my $part = $try[0] || "*";
403 $part .= '*' unless $part eq '*' || $part eq $try;
404 dbg("Compound prefix: $try $part" );
406 if (@try && $try eq $try[0]) {
408 lru_put($call, \@try);
412 lru_put($call, \@nout);
417 lru_put($call, \@nout);
425 @nout = matchprefix('Q');
427 lru_put($call, \@nout);
431 if (isdbg('prefixdata')) {
432 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
439 # turn a list of prefixes / dxcc numbers into a list of dxcc/itu/zone numbers
453 if ($cmd ne 'ns' && $v =~ /^\d+$/) {
454 push @out, $v unless grep $_ eq $v, @out;
456 if ($cmd eq 'ns' && $v =~ /^[A-Z][A-Z]$/i) {
457 push @out, uc $v unless grep $_ eq uc $v, @out;
459 my @pre = Prefix::extract($v);
462 foreach my $p (@pre) {
463 my $n = $p->dxcc if $cmd eq 'nc' ;
464 $n = $p->itu if $cmd eq 'ni' ;
465 $n = $p->cq if $cmd eq 'nz' ;
466 $n = $p->state if $cmd eq 'ns';
467 push @out, $n unless grep $_ eq $n, @out;
476 # get the full country data (dxcc, itu, cq, state, city) as a list
482 my @dxcc = extract($call);
484 my $state = $dxcc[1]->state || '';
485 my $city = $dxcc[1]->city || '';
486 my $name = $dxcc[1]->name || '';
488 return ($dxcc[1]->dxcc, $dxcc[1]->itu, $dxcc[1]->cq, $state, $city, $name);
490 return (666,0,0,'','','Pirate-Country-QQ');
494 lat => '0,Latitude,slat',
495 long => '0,Longitude,slong',
502 utcoff => '0,UTC offset',
503 cont => '0,Continent',
509 my $name = $AUTOLOAD;
511 return if $name =~ /::DESTROY$/;
514 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
515 # this clever line of code creates a subroutine which takes over from autoload
516 # from OO Perl - Conway
517 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
522 # return a prompt for a field
527 my ($self, $ele) = @_;