Tools/MSReader.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Tools/BISG.pm ↑ Summary Tools/IMP.pm »
1 package EBook::Tools::MSReader;
2 1 ▶use warnings; use strict; use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use version 0.74; our $VERSION = qv("0.5.0");
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Perl Critic overrides:
7 ## no critic (Package variable)
8 # RequireBriefOpen seems to be way too brief to be useful
9 ## no critic (RequireBriefOpen)
10 # Double-sigils are needed for lexical filehandles in clear print statements
11 ## no critic (Double-sigil dereference)
12  
13 =head1 NAME
14  
15 EBook::Tools::MSReader - Helper code for working with Microsoft Reader (.lit) e-books.
16  
17 =head1 SYNOPSIS
18  
19 use EBook::Tools::MSReader qw(find_convertlit find_convertlit_keys
20 system_convertlit);
21 $EBook::Tools::MSReader::convertlit_cmd = '/opt/convertlit/clit';
22 $EBook::Tools::MSReader::convertlit_keys = '/opt/convertlit/keys.txt';
23  
24 my $convertlit = find_convertlit();
25 my $keyfile = find_convertlit_keys();
26 system_convertlit(infile => 'myfile.lit',
27 dir => 'myfile-unpacked');
28  
29 =cut
30  
31 require Exporter;
32 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 our @EXPORT_OK;
35 @EXPORT_OK = qw (
36 &find_convertlit
37 &find_convertlit_keys
38 &system_convertlit
39 );
40 our %EXPORT_TAGS = ('all' => [@EXPORT_OK]);
41  
42 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 0 ▶use EBook::Tools qw(debug userconfigdir);
Subroutine BEGIN: called CC=1 SCAR=2.6
44 use Encode;
45 use File::Basename qw(dirname fileparse);
46 use File::Path; # Exports 'mkpath' and 'rmtree'
47 binmode(STDERR,':encoding(UTF-8)');
48  
49 my $drmsupport = 0;
50 eval
51 {
52 require EBook::Tools::DRM;
53 EBook::Tools::DRM->import();
54 }; # Trailing semicolon is required here
55 unless($@){ $drmsupport = 1; }
56  
57  
58 our $convertlit_cmd = '';
59 our $convertlit_keys = '';
60  
61  
62 ################################
63 ########## PROCEDURES ##########
64 ################################
65  
66 =head1 PROCEDURES
67  
68 All procedures are exportable, but none are exported by default.
69  
70  
71 =head2 C<find_convertlit()>
72  
73 Attempts to locate the convertlit executable by making a test
74 execution on predicted locations (including just checking PATH) and
75 looking in the EBook::Tools user configuration directory (see
76 L<EBook::Tools/userconfigdir()>.
77  
78 Returns the system command used for a successful invocation, or undef
79 if nothing worked.
80  
81 This will use package variable C<$convertlit_cmd> as its first guess,
82 and set that variable to the return value as well.
83  
84 =cut
85  
86 sub find_convertlit
87 {
88 my $subname = ( caller(0) )[3];
89 debug(2,"DEBUG[",$subname,"]");
90  
91 my @convertlit_guesses;
92 my $retval;
93 my $confdir = userconfigdir();
94  
95 if($OSNAME eq 'MSWin32')
96 {
97 @convertlit_guesses = (
98 'clit',
99 'C:\Program Files\ConvertLIT\clit',
100 );
101 if($confdir)
102 {
103 push(@convertlit_guesses,
104 $confdir . '\clit',
105 $confdir . '\convertlit');
106 }
107 }
108 else
109 {
110 @convertlit_guesses = (
111 'clit',
112 'convertlit',
113 );
114 if($confdir)
115 {
116 push(@convertlit_guesses,
117 $confdir . "/clit",
118 $confdir . "/convertlit");
119 }
120 push(@convertlit_guesses,
121 '/opt/convertlit/clit',
122 '/opt/convertlit/convertlit',
123 '/opt/clit/clit',
124 '/opt/clit/convertlit'
125 );
126 }
127 unshift(@convertlit_guesses,$convertlit_cmd)
128 if($convertlit_cmd);
129 undef($convertlit_cmd);
130  
131 foreach my $guess (@convertlit_guesses)
132 {
133 no warnings 'exec';
134 `$guess`;
135 # MS Windows may use 256 for a not-found code instead of -1
136 if($? != -1 && $? != 256)
137 {
138 debug(2,'DEBUG: `',$guess,'` returned ',$?);
139 $convertlit_cmd = $guess;
140 last;
141 }
142 }
143  
144 if($convertlit_cmd)
145 {
146 debug(1,"DEBUG: Found convertlit as '",$convertlit_cmd,"'");
147 return $convertlit_cmd;
148 }
149 else { return; }
150 }
151  
152  
153 =head2 C<find_convertlit_keys($filename)>
154  
155 Attempts to locate the convertlit C<keys.txt> file by checking
156 predicted filenames, both in the current working directory and in the
157 EBook::Tools user configuration directory (see
158 L<EBook::Tools/userconfigdir()>.
159  
160 If C<$filename> is provided, the file C<basename-keys.txt> will also
161 be checked in both locations.
162  
163 Returns the name of the first file found, or undef if nothing was found.
164  
165 This will use package variable C<$convertlit_keys> as its first guess,
166 and set that variable to the return value as well.
167  
168 =cut
169  
170 sub find_convertlit_keys
171 {
172 my $filename = shift;
173 my $subname = ( caller(0) )[3];
174 debug(2,"DEBUG[",$subname,"]");
175  
176 my $basename;
177 $basename = fileparse($filename,'\.\w+$') if($filename);
178 my $confdir = userconfigdir();
179 my $keyfile;
180  
181 my @guesses = ( 'keys.txt' );
182 push(@guesses, $basename . '-keys.txt') if($basename);
183 push(@guesses, $confdir . '/keys.txt') if($confdir);
184 push(@guesses, $confdir . '/' . $basename . '-keys.txt')
185 if($basename && $confdir);
186 unshift(@guesses,$convertlit_keys) if($convertlit_keys);
187  
188 foreach my $guess (@guesses)
189 {
190 if(-f $guess)
191 {
192 $keyfile = $guess;
193 debug(1,"DEBUG: found convertlit keys in '",$keyfile,"'");
194 last;
195 }
196 }
197 if($keyfile)
198 {
199 $convertlit_keys = $keyfile;
200 return $keyfile;
201 }
202 else { return; }
203 }
204  
205  
206 =head2 C<system_convertlit(%args)>
207  
208 Runs C<convertlit> to extract or downconvert a MS Reader .lit file.
209 The procedures L<find_convertlit()> and L<find_convertlit_keys()> are
210 both called to locate necessary helper files.
211  
212 Returns the return value from convertlit, or undef if convertlit or
213 the input file could not be found, or neither output file nor
214 directory is specified.
215  
216 =head3 Arguments
217  
218 =over
219  
220 =item * C<infile>
221  
222 The input filename. If not specified or invalid, the procedure croaks.
223  
224 =item * C<outfile>
225  
226 The output filename. If this is specified convertlit will perform a
227 downconversion.
228  
229 =item * C<dir>
230  
231 The output directory. If this is specified, and C<outfile> is not,
232 convertlit will perform an extraction. If both this and C<outfile>
233 are specified, convertlit will downconvert and place the downconverted
234 file into the specified directory.
235  
236 =item * C<keyfile>
237  
238 The location of the C<keys.txt> file containing the encryption keys,
239 if available. This is only required if the C<.lit> file is
240 DRM-protected and package variable C<$convertlit_keys> does not point
241 to the correct file.
242  
243 =back
244  
245 =cut
246  
247 sub system_convertlit
248 {
249 my %args = @_;
250 my $subname = ( caller(0) )[3];
251 debug(2,"DEBUG[",$subname,"]");
252  
253 my %valid_args = (
254 'infile' => 1,
255 'outfile' => 1,
256 'keyfile' => 1,
257 'dir' => 1,
258 );
259 foreach my $arg (keys %args)
260 {
261 croak($subname,"(): invalid argument '",$arg,"'")
262 if(!$valid_args{$arg});
263 }
264  
265 if(!$args{infile})
266 {
267 debug(1,$subname,"(): no input file specified!");
268 return;
269 }
270 if(! -f $args{infile})
271 {
272 debug(1,$subname,"(): input file '",$args{infile},"' not found!");
273 return;
274 }
275  
276 find_convertlit();
277 find_convertlit_keys();
278 croak($subname,"(): convertlit command not specified!\n")
279 unless($convertlit_cmd);
280  
281 my @convertlit = ($convertlit_cmd);
282 my $retval;
283 my $keyfile = $args{keyfile} || $convertlit_keys;
284 my $outfile = $args{outfile};
285 my $dir = $args{dir};
286  
287 if($keyfile)
288 {
289 push(@convertlit,"-k$keyfile");
290 }
291  
292 push(@convertlit,$args{infile});
293  
294 if($outfile && $dir)
295 {
296 push(@convertlit,"$dir/$outfile");
297 }
298 elsif($outfile)
299 {
300 push(@convertlit,$outfile);
301 }
302 elsif($dir)
303 {
304 # Expansion into a directory requires a trailing slash
305 unless($dir =~ m{(/ | \\) $}x)
306 {
307 $dir .= '/';
308 }
309 push(@convertlit,$dir);
310 }
311 else
312 {
313 debug(1,$subname,"(): neither output file nor directory specified!");
314 return;
315 }
316  
317 debug(2,"DEBUG: converting lit with '",join(' ',@convertlit),"'");
318 $retval = system(@convertlit);
319 return $retval;
320 }
321  
322 ########## END CODE ##########
323  
324 =head1 BUGS AND LIMITATIONS
325  
326 =over
327  
328 =item * All handling happens through ConvertLIT as an external helper.
329 Native Perl code may eventually be written to handle non-DRMed
330 extraction.
331  
332 =item * Unit tests are unwritten
333  
334 =back
335  
336 =head1 AUTHOR
337  
338 Zed Pobre <zed@debian.org>
339  
340 =head1 LICENSE AND COPYRIGHT
341  
342 Copyright 2008 Zed Pobre
343  
344 Licensed to the public under the terms of the GNU GPL, version 2.
345  
346 ConvertLIT (not included) is copyright 2002, 2003 Dan A. Jackson, and
347 licensed under the terms of the GNU GPL, version 2 or later.
348  
349 =cut
350  
351 1;
352 __END__
« Tools/BISG.pm ↑ Summary Tools/IMP.pm »