File Coverage

blib/lib/GnuPG/Options.pm
Criterion Covered Total %
statement 80 82 97.5
branch 42 66 63.6
path n/a
condition 1 2 50.0
subroutine 13 13 100.0
pod 2 5 40.0
total 138 168 82.1


line stmt bran path cond sub pod time code
1               # Options.pm
2               # - providing an object-oriented approach to GnuPG's options
3               #
4               # Copyright (C) 2000 Frank J. Tobin <ftobin@cpan.org>
5               #
6               # This module is free software; you can redistribute it and/or modify it
7               # under the same terms as Perl itself.
8               #
9               # This program is distributed in the hope that it will be useful,
10               # but WITHOUT ANY WARRANTY; without even the implied warranty of
11               # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12               #
13               # $Id: Options.pm,v 1.14 2001/08/21 13:31:50 ftobin Exp $
14               #
15                
16               package GnuPG::Options;
17 71       71   570 use Moo;
  71           190  
  71           731  
18 71       71   29605 use MooX::late;
  71           182  
  71           796  
19 71       71   54591 use MooX::HandlesVia;
  71           737020  
  71           493  
20               with qw(GnuPG::HashInit);
21                
22 71           6518 use constant BOOLEANS => qw(
23               armor
24               no_greeting
25               verbose
26               no_verbose
27               quiet
28               batch
29               always_trust
30               rfc1991
31               openpgp
32               force_v3_sigs
33               no_options
34               textmode
35               meta_pgp_5_compatible
36               meta_pgp_2_compatible
37               meta_interactive
38               ignore_mdc_error
39               keyring
40               no_default_keyring
41 71       71   10652 );
  71           175  
42                
43 71           5035 use constant SCALARS => qw(
44               homedir
45               default_key
46               comment
47               status_fd
48               logger_fd
49               passphrase_fd
50               command_fd
51               compress_algo
52               options
53               meta_signing_key
54               meta_signing_key_id
55               debug_level
56               logger_file
57 71       71   540 );
  71           171  
58                
59 71           14790 use constant LISTS => qw(
60               encrypt_to
61               recipients
62               meta_recipients_keys
63               meta_recipients_key_ids
64               extra_args
65 71       71   520 );
  71           166  
66                
67               has $_ => (
68               isa => 'Bool',
69               is => 'rw',
70               clearer => 'clear_' . $_,
71               ) for BOOLEANS;
72                
73               has $_ => (
74               isa => 'Any',
75               is => 'rw',
76               clearer => 'clear_' . $_,
77               ) for SCALARS;
78                
79               for my $list (LISTS) {
80               my $ref = $list . "_ref";
81               has $ref => (
82               handles_via => 'Array',
83               is => 'rw',
84               lazy => 1,
85               clearer => "clear_$list",
86               default => sub { [] },
87               handles => {
88               "push_$list" => 'push',
89               },
90               );
91                
92 71       71   693 no strict 'refs';
  71           195  
  71           60061  
93               *{$list} = sub {
94 330       330   1924 my $self = shift;
95 330 100         2561 return wantarray ? @{$self->$ref(@_)} : $self->$ref(@_);
  250           10059  
96               };
97               }
98                
99               sub BUILD {
100 85       85 0 1307075 my ( $self, $args ) = @_;
101               # Newer GnuPG will force failure for old ciphertext unless set
102 85     50     1306 $args->{ignore_mdc_error} //= 1;
103                
104 85           1026 $self->hash_init( meta_interactive => 1 );
105 85           6686 $self->hash_init(%$args);
106               }
107                
108               sub copy {
109 8       8 1 158 my ($self) = @_;
110                
111 8           326 my $new = ( ref $self )->new();
112                
113 8           424 foreach my $field ( BOOLEANS, SCALARS, LISTS ) {
114 288           8336 my $value = $self->$field();
115 288 100         2604 next unless defined $value;
116 88           1052 $new->$field($value);
117               }
118                
119 8           100 return $new;
120               }
121                
122               sub get_args {
123 50       50 1 3319 my ($self) = @_;
124                
125               return (
126 50           857 $self->get_meta_args(),
127               $self->get_option_args(),
128               $self->extra_args(),
129               );
130               }
131                
132               sub get_option_args {
133 50       50 0 174 my ($self) = @_;
134                
135 50           121 my @args = ();
136                
137 50 100         2787 push @args, '--homedir', $self->homedir() if $self->homedir();
138 50 50         4114 push @args, '--options', $self->options() if $self->options();
139 50 50         3077 push @args, '--no-options' if $self->no_options();
140 50 100         4288 push @args, '--armor' if $self->armor();
141 50 50         3031 push @args, '--textmode' if $self->textmode();
142 50 50         2632 push @args, '--default-key', $self->default_key() if $self->default_key();
143 50 50         3017 push @args, '--no-greeting' if $self->no_greeting();
144 50 50         2759 push @args, '--verbose' if $self->verbose();
145 50 50         2678 push @args, '--no-verbose' if $self->no_verbose();
146 50 50         3231 push @args, '--quiet' if $self->quiet();
147 50 50         2587 push @args, '--batch' if $self->batch();
148 50 100         2847 push @args, '--trust-model=always' if $self->always_trust();
149 50 50         2982 push @args, '--comment', $self->comment() if defined $self->comment();
150 50 50         2365 push @args, '--force-v3-sigs' if $self->force_v3_sigs();
151 50 50         2135 push @args, '--rfc1991' if $self->rfc1991;
152 50 50         2106 push @args, '--openpgp' if $self->openpgp();
153 50 50         2350 push @args, '--compress-algo', $self->compress_algo()
154               if defined $self->compress_algo();
155                
156 50 100         2759 push @args, '--status-fd', $self->status_fd()
157               if defined $self->status_fd();
158 50 50         2338 push @args, '--logger-fd', $self->logger_fd()
159               if defined $self->logger_fd();
160 50 100         1919 push @args, '--passphrase-fd', $self->passphrase_fd()
161               if defined $self->passphrase_fd();
162 50 50         3080 push @args, '--command-fd', $self->command_fd()
163               if defined $self->command_fd();
164                
165 50           1452 push @args, map { ( '--recipient', $_ ) } $self->recipients();
  5           118  
166 50           665 push @args, map { ( '--encrypt-to', $_ ) } $self->encrypt_to();
  0           0  
167                
168 50 50         1582 push @args, '--debug-level', $self->debug_level() if ($self->debug_level);
169 50 50         2392 push @args, '--logger-file', $self->logger_file() if ($self->logger_file());
170                
171 50 50         2345 push @args, '--ignore-mdc-error' if ($self->ignore_mdc_error());
172 50 50         2398 push @args, '--keyring' if ( $self->keyring() );
173 50 50         1919 push @args, '--no-default-keyring' if ( $self->no_default_keyring() );
174                
175 50           1510 return @args;
176               }
177                
178               sub get_meta_args {
179 50       50 0 243 my ($self) = @_;
180                
181 50           162 my @args = ();
182                
183 50 50         2777 push @args, '--compress-algo', 1, '--force-v3-sigs'
184               if $self->meta_pgp_5_compatible();
185 50 50         2648 push @args, '--rfc1991' if $self->meta_pgp_2_compatible();
186 50 100         2949 push @args, '--batch', '--no-tty' if not $self->meta_interactive();
187                
188               # To eliminate confusion, we'll move to having any options
189               # that deal with keys end in _id(s) if they only take
190               # an id; otherwise we assume that a GnuPG::Key
191 50 100         4211 push @args, '--default-key', $self->meta_signing_key_id()
192               if $self->meta_signing_key_id();
193 50 50         4795 push @args, '--default-key', $self->meta_signing_key()->hex_id()
194               if $self->meta_signing_key();
195                
196               push @args,
197 50           2560 map { ( '--recipient', $_ ) } $self->meta_recipients_key_ids();
  0           0  
198               push @args,
199 50           708 map { ( '--recipient', $_->hex_id() ) } $self->meta_recipients_keys();
  1           78  
200                
201 50           461 return @args;
202               }
203                
204               1;
205                
206               __END__
207                
208               =head1 NAME
209                
210               GnuPG::Options - GnuPG options embodiment
211                
212               =head1 SYNOPSIS
213                
214               # assuming $gnupg is a GnuPG::Interface object
215               $gnupg->options->armor( 1 );
216               $gnupg->options->push_recipients( 'ftobin', '0xABCD1234ABCD1234ABCD1234ABCD1234ABCD1234' );
217                
218               =head1 DESCRIPTION
219                
220               GnuPG::Options objects are generally not instantiated on their
221               own, but rather as part of a GnuPG::Interface object.
222                
223               =head1 OBJECT METHODS
224                
225               =over 4
226                
227               =item new( I<%initialization_args> )
228                
229               This methods creates a new object. The optional arguments are
230               initialization of data members.
231                
232               =item hash_init( I<%args> ).
233                
234                
235               =item copy
236                
237               Returns a copy of this object. Useful for 'saving' options.
238                
239               =item get_args
240                
241               Returns a list of arguments to be passed to GnuPG based
242               on data members which are 'meta_' options, regular options,
243               and then I<extra_args>, in that order.
244                
245               =back
246                
247               =head1 OBJECT DATA MEMBERS
248                
249               =over 4
250                
251               =item homedir
252                
253               =item armor
254                
255               =item textmode
256                
257               =item default_key
258                
259               =item no_greeting
260                
261               =item verbose
262                
263               =item no_verbose
264                
265               =item quiet
266                
267               =item batch
268                
269               =item always_trust
270                
271               =item comment
272                
273               =item status_fd
274                
275               =item logger_fd
276                
277               =item passphrase_fd
278                
279               =item compress_algo
280                
281               =item force_v3_sigs
282                
283               =item rfc1991
284                
285               =item openpgp
286                
287               =item options
288                
289               =item no_options
290                
291               =item encrypt_to
292                
293               =item recipients
294                
295               =back
296                
297               These options correlate directly to many GnuPG options. For those that
298               are boolean to GnuPG, simply that argument is passed. For those
299               that are associated with a scalar, that scalar is passed passed
300               as an argument appropriate. For those that can be specified more
301               than once, such as B<recipients>, those are considered lists
302               and passed accordingly. Each are undefined or false to begin.
303                
304               =head2 Meta Options
305                
306               Meta options are those which do not correlate directly to any
307               option in GnuPG, but rather are generally a bundle of options
308               used to accomplish a specific goal, such as obtaining
309               compatibility with PGP 5. The actual arguments each of these
310               reflects may change with time. Each defaults to false unless
311               otherwise specified.
312                
313               These options are being designed and to provide a non-GnuPG-specific
314               abstraction, to help create compatibility with a possible
315               PGP::Interface module.
316                
317               To help avoid confusion, methods with take a form of a key as
318               an object shall be prepended with I<_id(s)> if they only
319               take an id; otherwise assume an object of type GnuPG::Key
320               is required.
321                
322               =over 4
323                
324               =item meta_pgp_5_compatible
325                
326               If true, arguments are generated to try to be compatible with PGP 5.x.
327                
328               =item meta_pgp_2_compatible
329                
330               If true, arguments are generated to try to be compatible with PGP 2.x.
331                
332               =item meta_interactive
333                
334               If false, arguments are generated to try to help the using program
335               use GnuPG in a non-interactive environment, such as CGI scripts.
336               Default is true.
337                
338               =item meta_signing_key_id
339                
340               This scalar reflects the key used to sign messages.
341               Currently this is synonymous with I<default-key>.
342                
343               =item meta_signing_key
344                
345               This GnuPG::Key object reflects the key used to sign messages.
346                
347               =item meta_recipients_key_ids
348                
349               This list of scalar key ids are used to generate the
350               appropriate arguments having these keys as recipients.
351                
352               =item meta_recipients_keys
353                
354               This list of keys of the type GnuPG::Key are used to generate the
355               appropriate arguments having these keys as recipients.
356               You probably want to have this list be of the inherited class
357               GnuPG::SubKey, as in most instances, OpenPGP keypairs have
358               the encyrption key as the subkey of the primary key, which is
359               used for signing.
360                
361               =back
362                
363               =head2 Other Data Members
364                
365               =over 4
366                
367               =item extra_args
368                
369               This is a list of any other arguments used to pass to GnuPG.
370               Useful to pass an argument not yet covered in this package.
371                
372               =back
373                
374               =head1 SEE ALSO
375                
376               L<GnuPG::Interface>,
377                
378               =cut