4 # Copyright (c) - Dirk Koopman G1TLH
22 use vars qw($db %prefix_loc %pre $lru $lrusize $misses $hits $matchtotal);
24 $db = undef; # the DB_File handle
25 %prefix_loc = (); # the meat of the info
26 %pre = (); # the prefix list
27 $hits = $misses = $matchtotal = 1; # cache stats
28 $lrusize = 1000; # size of prefix LRU cache
35 # fix up the node's default country codes
36 unless (@main::my_cc) {
37 push @main::my_cc, (61..67) if $main::mycall =~ /^GB/;
38 push @main::my_cc, qw(EA EA6 EA8 EA9) if $main::mycall =~ /^E[ABCD]/;
39 push @main::my_cc, qw(I IT IS) if $main::mycall =~ /^I/;
40 push @main::my_cc, qw(SV SV5 SV9) if $main::mycall =~ /^SV/;
43 push @main::my_cc, $main::mycall unless @main::my_cc;
51 my @dxcc = extract($_);
52 push @c, $dxcc[1]->dxcc if @dxcc > 1;
55 return "\@main::my_cc does not contain a valid prefix or callsign (" . join(',', @main::my_cc) . ")" unless @c;
72 # tie the main prefix database
73 eval {$db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0664, $DB_BTREE);};
74 my $out = "$@($!)" if !$db || $@ ;
75 my $fn = localdata("prefix_data.pl");
76 die "Prefix.pm: cannot find $fn, have you run /spider/perl/create_prefix.pl?" unless -e $fn;
78 eval {do $fn if !$out; };
80 $lru = LRU->newbase('Prefix', $lrusize);
93 my $fh = new IO::File;
94 my $fn = localdata("prefix_data.pl");
96 confess "Prefix system not started" if !$db;
99 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
100 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
101 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
102 rename "$fn.o", "$fn.oo" if -e "$fn.o";
103 rename "$fn", "$fn.o" if -e "$fn";
105 $fh->open(">$fn") or die "Can't open $fn ($!)";
107 # prefix location data
108 $fh->print("%prefix_loc = (\n");
109 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
110 my $r = $prefix_loc{$l};
111 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
112 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
114 $fh->print(");\n\n");
117 $fh->print("%pre = (\n");
118 foreach $k (sort keys %pre) {
119 $fh->print(" '$k' => [");
120 my @list = @{$pre{$k}};
127 $fh->print("$str ],\n");
134 # what you get is a list that looks like:-
136 # prefix => @list of blessed references to prefix_locs
138 # This routine will only do what you ask for, if you wish to be intelligent
139 # then that is YOUR problem!
147 return () if $db->seq($gotkey, $ref, R_CURSOR);
148 return () if $key ne substr $gotkey, 0, length $key;
150 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
154 # get the next key that matches, this assumes that you have done a 'get' first
163 return () if $db->seq($gotkey, $ref, R_NEXT);
164 return () if $key ne substr $gotkey, 0, length $key;
166 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
170 # put the key LRU incluing the city state info
175 my ($call, $ref) = @_;
177 my @s = USDB::get($call);
180 # this is deep magic, because this is a reference to static data, it
182 my $h = { %{$ref->[1]} };
183 bless $h, ref $ref->[1];
188 $ref->[1]->{city} = $ref->[1]->{state} = "" unless exists $ref->[1]->{state};
191 dbg("Prefix::lru_put $call -> ($ref->[1]->{city}, $ref->[1]->{state})") if isdbg('prefix');
192 $lru->put($call, $ref);
196 # search for the nearest match of a prefix string (starting
197 # from the RH end of the string passed)
205 for (my $i = length $pref; $i; $i--) {
207 my $s = substr($pref, 0, $i);
209 my $p = $lru->get($s);
212 if (isdbg('prefix')) {
213 my $percent = sprintf "%.1f", $hits * 100 / $misses;
214 dbg("Partial Prefix Cache Hit: $s Hits: $hits/$misses of $matchtotal = $percent\%");
216 lru_put($_, $p) for @partials;
221 if (isdbg('prefix')) {
222 my $part = $out[0] || "*";
223 $part .= '*' unless $part eq '*' || $part eq $s;
224 dbg("Partial prefix: $pref $s $part" );
226 if (@out && $out[0] eq $s) {
235 # extract a 'prefix' from a callsign, in other words the largest entity that will
236 # obtain a result from the prefix table.
238 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
239 # G1TLH/VO1 (should) return VO1
244 my $calls = uc shift;
250 LM: foreach $call (split /,/, $calls) {
253 $call =~ s/-\d+$//; # ignore SSIDs
255 my $ecall = "=$call";
257 # first check if this is a call (by prefixing it with an = sign)
258 my $p = $lru->get($ecall);
261 if (isdbg('prefix')) {
262 my $percent = sprintf "%.1f", $hits * 100 / $misses;
263 dbg("Prefix Exact Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
269 # then check if the whole thing succeeds either because it is cached
270 # or because it simply is a stored prefix as callsign (or even a prefix)
271 $p = $lru->get($call);
274 if (isdbg('prefix')) {
275 my $percent = sprintf "%.1f", $hits * 100 / $misses;
276 dbg("Prefix Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
282 # is it in the USDB, force a matchprefix to match?
283 my @s = USDB::get($call);
286 @nout = matchprefix($call) unless @nout;
287 $nout[0] = $ecall if @nout;
290 # try a straight get for an exact callsign
294 # now store the exact prefix if it has been found
295 if (@nout && $nout[0] eq $ecall) {
298 lru_put("=$call", \@nout);
299 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
304 # now try a non-exact call/prefix
305 if ((@nout = get($call)) && $nout[0] eq $call) {
307 lru_put($call, \@nout);
308 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
313 # now split the call into parts if required
314 @parts = ($call =~ '/') ? split('/', $call) : ($call);
315 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
317 # remove any /0-9 /P /A /M /MM /AM suffixes etc
319 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
321 # can we resolve them by direct lookup
322 my $s = join('/', @parts);
324 if (@nout && $nout[0] eq $s) {
325 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
327 lru_put($call, \@nout);
332 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
334 # at this point we should have two or three parts
335 # if it is three parts then join the first and last parts together
338 # first deal with prefix/x00xx/single letter things
339 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
340 @nout = matchprefix($parts[0]);
342 my $s = join('/', $nout[0], $parts[2]);
344 if (@try && $try[0] eq $s) {
345 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
347 lru_put($call, \@try);
352 # if the second part is a callsign and the last part is one letter
353 if (is_callsign($parts[1]) && length $parts[2] == 1) {
359 # if it is a two parter
362 # try it as it is as compound, taking the first part as the prefix
363 @nout = matchprefix($parts[0]);
365 my $s = join('/', $nout[0], $parts[1]);
367 if (@try && $try[0] eq $s) {
368 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
370 lru_put($call, \@try);
377 # remove the problematic /J suffix
378 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
382 @nout = matchprefix($parts[0]);
384 dbg("got prefix: $call = $nout[0]") if isdbg('prefix');
386 lru_put($call, \@nout);
395 L1: for ($n = 0; $n < @parts; $n++) {
398 for ($i = $k = 0; $i < @parts; $i++) {
399 next if $checked[$i];
401 if (!$sp || length $p < length $sp) {
402 dbg("try part: $p") if isdbg('prefix');
408 $sp =~ s/-\d+$//; # remove any SSID
410 # now start to resolve it from the right hand end
411 @nout = matchprefix($sp);
413 # try and search for it in the descriptions as
414 # a whole callsign if it has multiple parts and the output
415 # is more two long, this should catch things like
416 # FR5DX/T without having to explicitly stick it into
421 $parts[$k] = $nout[0];
422 my $try = join('/', @parts);
424 if (isdbg('prefix')) {
425 my $part = $try[0] || "*";
426 $part .= '*' unless $part eq '*' || $part eq $try;
427 dbg("Compound prefix: $try $part" );
429 if (@try && $try eq $try[0]) {
431 lru_put($call, \@try);
435 lru_put($call, \@nout);
440 lru_put($call, \@nout);
448 @nout = matchprefix('QQ');
450 lru_put($call, \@nout);
454 if (isdbg('prefixdata')) {
455 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
462 # turn a list of prefixes / dxcc numbers into a list of dxcc/itu/zone numbers
476 if ($cmd ne 'ns' && $v =~ /^\d+$/) {
477 push @out, $v unless grep $_ eq $v, @out;
479 if ($cmd eq 'ns' && $v =~ /^[A-Z][A-Z]$/i) {
480 push @out, uc $v unless grep $_ eq uc $v, @out;
482 my @pre = Prefix::extract($v);
485 foreach my $p (@pre) {
486 my $n = $p->dxcc if $cmd eq 'nc' ;
487 $n = $p->itu if $cmd eq 'ni' ;
488 $n = $p->cq if $cmd eq 'nz' ;
489 $n = $p->state if $cmd eq 'ns';
490 push @out, $n unless grep $_ eq $n, @out;
499 # get the full country data (dxcc, itu, cq, state, city) as a list
505 my @dxcc = extract($call);
507 my $state = $dxcc[1]->state || '';
508 my $city = $dxcc[1]->city || '';
509 my $name = $dxcc[1]->name || '';
511 return ($dxcc[1]->dxcc, $dxcc[1]->itu, $dxcc[1]->cq, $state, $city, $name);
513 return (666,0,0,'','','Pirate-Country-QQ');
517 lat => '0,Latitude,slat',
518 long => '0,Longitude,slong',
525 utcoff => '0,UTC offset',
526 cont => '0,Continent',
532 my $name = $AUTOLOAD;
534 return if $name =~ /::DESTROY$/;
537 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
538 # this clever line of code creates a subroutine which takes over from autoload
539 # from OO Perl - Conway
540 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
545 # return a prompt for a field
550 my ($self, $ele) = @_;