X-Git-Url: http://dxcluster.org/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FQSL.pm;h=5101f25f6c561ca3ebfc67a6acf62a467bb0d536;hb=846aa525969cab9b37936fb33b8705a68fd52886;hp=d7dc8b2c0fced88746f56db95bd98de9c994160c;hpb=e07645cec07ba739a20cc009d7dd138c962b66eb;p=spider.git diff --git a/perl/QSL.pm b/perl/QSL.pm index d7dc8b2c..5101f25f 100644 --- a/perl/QSL.pm +++ b/perl/QSL.pm @@ -2,7 +2,7 @@ # # Local 'autoqsl' module for DXSpider # -# Copyright (c) 2003 Dirk Koopman G1TLH +# Copyright (c) 2003-2020 Dirk Koopman G1TLH # package QSL; @@ -14,35 +14,27 @@ use DB_File; use DXDebug; use Prefix; -use vars qw($VERSION $BRANCH); -$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ ); -$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0)); -$main::build += $VERSION; -$main::branch += $BRANCH; +use JSON; +use Data::Structure::Util qw(unbless); -use vars qw($qslfn $dbm); -$qslfn = 'qsl'; +use vars qw($qslfn $dbm $maxentries); +$qslfn = 'dxqsl'; $dbm = undef; +$maxentries = 10; + +my $json; +my %u; sub init { my $mode = shift; - my $ufn = "$main::root/data/$qslfn.v1"; + my $ufn = localdata("$qslfn.v2"); Prefix::load() unless Prefix::loaded(); - - eval { - require Storable; - }; - - if ($@) { - dbg("Storable appears to be missing"); - dbg("In order to use the QSL feature you must"); - dbg("load Storable from CPAN"); - return undef; - } - import Storable qw(nfreeze freeze thaw); - my %u; + $json = JSON->new->canonical(1); + + untie %u; + undef $dbm; if ($mode) { $dbm = tie (%u, 'DB_File', $ufn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open qsl file: $qslfn ($!)"; } else { @@ -53,16 +45,20 @@ sub init sub finish { + untie %u; undef $dbm; } sub new { my ($pkg, $call) = @_; + return undef if $call =~ /INFO|QSL|VIA/; return bless [uc $call, []], $pkg; } -# the format of each entry is [manager, times found, last time] +# called $self->update(comment, time, spotter) +# $self has the callsign as the first argument in an array of array references +# the format of each entry is [manager, times found, last time, last reporter] sub update { return unless $dbm; @@ -71,19 +67,26 @@ sub update my $t = shift; my $by = shift; my $changed; - + + return unless length $line && $line =~ /\b(?:QSL|VIA|BUR[OE]?A?U?|OQRS|LOTW)\b/i; foreach my $man (split /\b/, uc $line) { my $tok; - if (is_callsign($man)) { + if (is_callsign($man) && !is_qra($man)) { my @pre = Prefix::extract($man); $tok = $man if @pre && $pre[0] ne 'Q'; } elsif ($man =~ /^BUR/) { $tok = 'BUREAU'; + } elsif ($man =~ /^LOTW/) { + $tok = 'LOTW'; + } elsif ($man =~ /^OQRS/) { + $tok = 'OQRS'; } elsif ($man eq 'HC' || $man =~ /^HOM/ || $man =~ /^DIR/) { $tok = 'HOME CALL'; } elsif ($man =~ /^QRZ/) { $tok = 'QRZ.com'; + } else { + next; } if ($tok) { my ($r) = grep {$_->[0] eq $tok} @{$self->[1]}; @@ -99,6 +102,8 @@ sub update unshift @{$self->[1]}, $r; $changed++; } + # prune the number of entries + pop @{$self->[1]} while (@{$self->[1]} > $maxentries); } } $self->put if $changed; @@ -108,11 +113,11 @@ sub get { return undef unless $dbm; my $key = uc shift; + my $value; - my $r = $dbm->get($key, $value); return undef if $r; - return thaw($value); + return json_decode($value); } sub put @@ -120,8 +125,30 @@ sub put return unless $dbm; my $self = shift; my $key = $self->[0]; - my $value = nfreeze($self); + my $value = json_encode($self); $dbm->put($key, $value); } +sub json_decode +{ + my $s = shift; + my $ref; + eval { $ref = $json->decode($s) }; + if ($ref && !$@) { + return bless $ref, __PACKAGE__; + } else { + LogDbg('DXUser', "__PACKAGE_::json_decode: on '$s' $@"); + } + return undef; +} + +sub json_encode +{ + my $ref = shift; + unbless($ref); + my $s = $json->encode($ref); + bless $ref, __PACKAGE__; + return $s; +} + 1;