fix console.pl
[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 %u;
25 my $json;
26
27 localdata_mv("$qslfn.v1j");
28
29 sub init
30 {
31         my $mode = shift;
32         my $ufn = localdata("$qslfn.v1j");
33
34         $json = JSON->new->canonical(1);
35         
36         Prefix::load() unless Prefix::loaded();
37
38         finish() if $dbm;
39         
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         $dbm->sync;
51         undef $dbm;
52         untie %u;
53 }
54
55 sub new
56 {
57         my ($pkg, $call) = @_;
58         return bless [uc $call, []], $pkg;
59 }
60
61 # called $self->update(comment, time, spotter)
62 # $self has the callsign as the first argument in an array of array references
63 # the format of each entry is [manager, times found, last time, last reporter]
64 sub update
65 {
66         return unless $dbm;
67         my $self = shift;
68         my $line = shift;
69         my $t = shift;
70         my $by = shift;
71         my $changed;
72
73         return unless length $line && $line =~ /\b(?:QSL|VIA)\b/i;
74         foreach my $man (split /\b/, uc $line) {
75                 my $tok;
76                 
77                 if (is_callsign($man) && !is_qra($man)) {
78                         my @pre = Prefix::extract($man);
79                         $tok = $man if @pre && $pre[0] ne 'Q';
80                 } elsif ($man =~ /^BUR/) {
81                         $tok = 'BUREAU';
82                 } elsif ($man =~ /^LOTW/) {
83                         $tok = 'LOTW';
84                 } elsif ($man eq 'HC' || $man =~ /^HOM/ || $man =~ /^DIR/) {
85                         $tok = 'HOME CALL';
86                 } elsif ($man =~ /^QRZ/) {
87                         $tok = 'QRZ.com';
88                 } else {
89                         next;
90                 }
91                 if ($tok) {
92                         my ($r) = grep {$_->[0] eq $tok} @{$self->[1]};
93                         if ($r) {
94                                 $r->[1]++;
95                                 if ($t > $r->[2]) {
96                                         $r->[2] = $t;
97                                         $r->[3] = $by;
98                                 }
99                                 $changed++;
100                         } else {
101                                 $r = [$tok, 1, $t, $by];
102                                 unshift @{$self->[1]}, $r;
103                                 $changed++;
104                         }
105                         # prune the number of entries
106                         pop @{$self->[1]} while (@{$self->[1]} > $maxentries);
107                 }
108         }
109         $self->put if $changed;
110 }
111
112 sub get
113 {
114         return undef unless $dbm;
115         my $key = uc shift;
116         my $value;
117         
118         my $r = $dbm->get($key, $value);
119         return undef if $r;
120         return decode($value);
121 }
122
123 sub put
124 {
125         return unless $dbm;
126         my $self = shift;
127         my $key = $self->[0];
128         my $value = encode($self);
129         $dbm->put($key, $value);
130 }
131
132 sub remove_files
133 {
134         unlink "$main::data/$qslfn.v1j";
135         unlink "$main::local_data/$qslfn.v1j";
136 }
137
138 # thaw the user
139 sub decode
140 {
141     my $s = shift;
142     my $ref;
143     eval { $ref = $json->decode($s) };
144     if ($ref && !$@) {
145         return bless $ref, 'QSL';
146     } 
147     return undef;
148 }
149
150 # freeze the user
151 sub encode
152 {
153     my $ref = shift;
154     unbless($ref);
155     my $s;
156         
157         eval {$s = $json->encode($ref) };
158         if ($s && !$@) {
159                 bless $ref, 'QSL';
160                 return $s;
161         } 
162 }
163
164 1;