File Coverage

blib/lib/Crypt/OpenPGP/Config.pm
Criterion Covered Total %
statement 63 63 100.0
branch 8 12 66.6
condition 3 6 50.0
subroutine 20 20 100.0
pod 0 4 0.0
total 94 105 89.5


line stmt bran cond sub pod time code
1             package Crypt::OpenPGP::Config;
2 11     11   91 use strict;
  11         24  
  11         434  
3 11     11   64 use warnings;
  11         18  
  11         821  
4              
5             our $VERSION = '1.19'; # VERSION
6              
7 11     11   68 use Crypt::OpenPGP::ErrorHandler;
  11         57  
  11         387  
8 11     11   61 use base qw( Crypt::OpenPGP::ErrorHandler );
  11         35  
  11         11689  
9              
10             sub new {
11 12     12 0 276743 my $class = shift;
12 12         153 my $cfg = bless { o => { @_ } }, $class;
13 12         233 $cfg;
14             }
15              
16 104     104 0 13240 sub get { $_[0]->{o}{ $_[1] } }
17             sub set {
18 4     4 0 13 my $cfg = shift;
19 4         19 my($key, $val) = @_;
20 4         18 $cfg->{o}{$key} = $val;
21             }
22              
23             {
24             my %STANDARD = (
25             str => \&_set_str,
26             bool => \&_set_bool,
27             );
28              
29             sub read_config {
30 4     4 0 1419 my $cfg = shift;
31 4         23 my($compat, $cfg_file) = @_;
32 4         22 my $class = join '::', __PACKAGE__, $compat;
33 4         52 my $directives = $class->directives;
34 4         61 local(*FH, $_, $/);
35 4         16 $/ = "\n";
36 4 50       289 open FH, $cfg_file or
37             return $cfg->error("Error opening file '$cfg_file': $!");
38 4         179 while () {
39 12         37 chomp;
40 12 50 33     100 next if !/\S/ || /^#/;
41 12 50       133 if (/^\s*([^\s=]+)(?:(?:(?:\s*=\s*)|\s+)(.*))?/) {
42 12         59 my($key, $val) = ($1, $2);
43 12         36 my $ref = $directives->{lc $key};
44 12 50       32 next unless $ref;
45             my $code = ref($ref->[0]) eq 'CODE' ? $ref->[0] :
46 12 100       70 $STANDARD{$ref->[0]};
47 12         40 $code->($cfg, $ref->[1], $val);
48             }
49             }
50 4         111 close FH;
51             }
52             }
53              
54 4     4   57 sub _set_str { $_[0]->{o}{$_[1]} = $_[2] }
55             {
56             my %BOOL = ( off => 0, on => 1 );
57             sub _set_bool {
58 4     4   18 my($cfg, $key, $val) = @_;
59 4 100       12 $val = 1 unless defined $val;
60 4   66     23 $val = $BOOL{$val} || $val;
61 4         46 $cfg->{o}{$key} = $val;
62             }
63             }
64              
65             package Crypt::OpenPGP::Config::GnuPG;
66 11     11   93 use strict;
  11         33  
  11         373  
67 11     11   76 use warnings;
  11         24  
  11         3419  
68             sub directives {
69             {
70 2     2   90 armor => [ 'bool', 'Armour' ],
71             'default-key' => [ 'str', 'DefaultKey' ],
72             recipient => [ 'str', 'Recipient' ],
73             'default-recipient' => [ 'str', 'DefaultRecipient' ],
74             'default-recipient-self' => [ 'bool', 'DefaultSelfRecipient' ],
75             'encrypt-to' => [ 'str', 'Recipient' ],
76             verbose => [ 'bool', 'Verbose' ],
77             textmode => [ 'bool', 'TextMode' ],
78             keyring => [ 'str', 'PubRing' ],
79             'secret-keyring' => [ 'str', 'SecRing' ],
80             'cipher-algo' => [ \&_set_cipher ],
81             'digest-algo' => [ 'str', 'Digest' ],
82             'compress-algo' => [ \&_set_compress ],
83             }
84             }
85              
86             {
87             my %Ciphers = (
88             '3DES' => 'DES3', BLOWFISH => 'Blowfish',
89             RIJNDAEL => 'Rijndael', RIJNDAEL192 => 'Rijndael192',
90             RIJNDAEL256 => 'Rijndael256', TWOFISH => 'Twofish',
91             CAST5 => 'CAST5',
92             );
93 2     2   95 sub _set_cipher { $_[0]->{o}{Cipher} = $Ciphers{$_[2]} }
94              
95             my %Compress = ( 1 => 'ZIP', 2 => 'Zlib' );
96 2     2   36 sub _set_compress { $_[0]->{o}{Compress} = $Compress{$_[2]} }
97             }
98              
99             package Crypt::OpenPGP::Config::PGP2;
100 11     11   83 use strict;
  11         28  
  11         11392  
101 11     11   94 use warnings;
  11         35  
  11         1360  
102             sub directives {
103             {
104 2     2   28 armor => [ 'bool', 'Armour' ],
105             compress => [ 'bool', 'Compress' ],
106             encrypttoself => [ 'bool', 'EncryptToSelf' ],
107             myname => [ 'str', 'DefaultSelfRecipient' ],
108             pubring => [ 'str', 'PubRing' ],
109             secring => [ 'str', 'SecRing' ],
110             }
111             }
112              
113             package Crypt::OpenPGP::Config::PGP5;
114 11     11   74 use strict;
  11         39  
  11         330  
115 11     11   51 use warnings;
  11         18  
  11         944  
116             *directives = \&Crypt::OpenPGP::Config::PGP2::directives;
117              
118             1;