Merge branch 'mojo' into users.v3j
[spider.git] / perl / QSL.pm
1 #!/usr/bin/perl -w
2 #
3 # Local 'autoqsl' module for DXSpider
4 #
5 # Copyright (c) 2003 Dirk Koopman G1TLH
6 #
7
8 package QSL;
9
10 use strict;
11 use SysVar;
12 use DXUtil;
13 use DB_File;
14 use DXDebug;
15 use Prefix;
16 use JSON;
17 use Data::Structure::Util qw(unbless);
18
19 use vars qw($qslfn $dbm $maxentries);
20 $qslfn = 'dxqsl';
21 $dbm = undef;
22 $maxentries = 50;
23
24 my $json;
25
26 localdata_mv("$qslfn.v1j");
27
28 sub init
29 {
30         my $mode = shift;
31         my $ufn = localdata("$qslfn.v1j");
32
33         $json = JSON->new->canonical(1);
34         
35         Prefix::load() unless Prefix::loaded();
36         
37
38         my %u;
39         undef $dbm;
40         if ($mode) {
41                 $dbm = tie (%u, 'DB_File', $ufn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open qsl file: $qslfn ($!)";
42         } else {
43                 $dbm = tie (%u, 'DB_File', $ufn, O_RDONLY, 0666, $DB_BTREE) or confess "can't open qsl file: $qslfn ($!)";
44         }
45         return $dbm;
46 }
47
48 sub finish
49 {
50         undef $dbm;
51 }
52
53 sub new
54 {
55         my ($pkg, $call) = @_;
56         return bless [uc $call, []], $pkg;
57 }
58
59 # called $self->update(comment, time, spotter)
60 # $self has the callsign as the first argument in an array of array references
61 # the format of each entry is [manager, times found, last time, last reporter]
62 sub update
63 {
64         return unless $dbm;
65         my $self = shift;
66         my $line = shift;
67         my $t = shift;
68         my $by = shift;
69         my $changed;
70
71         return unless length $line && $line =~ /\b(?:QSL|VIA)\b/i;
72         foreach my $man (split /\b/, uc $line) {
73                 my $tok;
74                 
75                 if (is_callsign($man) && !is_qra($man)) {
76                         my @pre = Prefix::extract($man);
77                         $tok = $man if @pre && $pre[0] ne 'Q';
78                 } elsif ($man =~ /^BUR/) {
79                         $tok = 'BUREAU';
80                 } elsif ($man =~ /^LOTW/) {
81                         $tok = 'LOTW';
82                 } elsif ($man eq 'HC' || $man =~ /^HOM/ || $man =~ /^DIR/) {
83                         $tok = 'HOME CALL';
84                 } elsif ($man =~ /^QRZ/) {
85                         $tok = 'QRZ.com';
86                 } else {
87                         next;
88                 }
89                 if ($tok) {
90                         my ($r) = grep {$_->[0] eq $tok} @{$self->[1]};
91                         if ($r) {
92                                 $r->[1]++;
93                                 if ($t > $r->[2]) {
94                                         $r->[2] = $t;
95                                         $r->[3] = $by;
96                                 }
97                                 $changed++;
98                         } else {
99                                 $r = [$tok, 1, $t, $by];
100                                 unshift @{$self->[1]}, $r;
101                                 $changed++;
102                         }
103                         # prune the number of entries
104                         pop @{$self->[1]} while (@{$self->[1]} > $maxentries);
105                 }
106         }
107         $self->put if $changed;
108 }
109
110 sub get
111 {
112         return undef unless $dbm;
113         my $key = uc shift;
114         my $value;
115         
116         my $r = $dbm->get($key, $value);
117         return undef if $r;
118         return decode($value);
119 }
120
121 sub put
122 {
123         return unless $dbm;
124         my $self = shift;
125         my $key = $self->[0];
126         my $value = encode($self);
127         $dbm->put($key, $value);
128 }
129
130 sub remove_files
131 {
132         unlink "$main::data/qsl.v1j";
133         unlink "$main::local_data/qsl.v1j";
134 }
135
136 # thaw the user
137 sub decode
138 {
139     my $s = shift;
140     my $ref;
141     eval { $ref = $json->decode($s) };
142     if ($ref && !$@) {
143         return bless $ref, 'QSL';
144     } 
145     return undef;
146 }
147
148 # freeze the user
149 sub encode
150 {
151     my $ref = shift;
152     unbless($ref);
153     my $s;
154         
155         eval {$s = $json->encode($ref) };
156         if ($s && !$@) {
157                 bless $ref, 'QSL';
158                 return $s;
159         } 
160 }
161
162 1;