I have an updated version of this patch with an additional test coming,
sorry for the extra noise, bear with me.
Tim
On Wed May 09 09:26:04 2012, TWILDE wrote:
Show quoted text> From: Tim Wilde <github@krellis.org>
>
> ---
> MANIFEST | 1 +
> lib/Mail/GnuPG.pm | 17 +++++++++--------
> t/35.has-public-key.t | 21 +++++++++++++++++++++
> 3 files changed, 31 insertions(+), 8 deletions(-)
> create mode 100644 t/35.has-public-key.t
>
> diff --git a/MANIFEST b/MANIFEST
> index 6750e81..1cb636a 100644
> --- a/MANIFEST
> +++ b/MANIFEST
> @@ -8,6 +8,7 @@ t/05.load.t
> t/20.inline-verify.t
> t/25.multipart-verify.t
> t/30.inline-decrypt.t
> +t/35.has-public-key.t
> t/99.pod.t
> t/agent.t
> t/base.t
> diff --git a/lib/Mail/GnuPG.pm b/lib/Mail/GnuPG.pm
> index ef12594..baff93e 100644
> --- a/lib/Mail/GnuPG.pm
> +++ b/lib/Mail/GnuPG.pm
> @@ -476,14 +476,15 @@ sub _rebuild_key_cache {
> my $self = shift;
> local $_;
> %key_cache = ();
> - # sometimes the best tool for the job... is not perl
> - open(my $fh, "$self->{gpg_path} --list-public-keys --with-colons |
> cut -d: -f10|")
> - or die $!;
> - while(<$fh>) {
> - next unless $_;
> - # M::A may not parse the gpg stuff properly. Cross fingers
> - my ($a) = Mail::Address->parse($_); # list context, please
> - $key_cache{$a->address}=1 if ref $a;
> + my $gnupg = GnuPG::Interface->new();
> + $self->_set_options($gnupg);
> + my @keys = $gnupg->get_public_keys();
> + foreach my $key (@keys) {
> + foreach my $uid ($key->user_ids) {
> + # M::A may not parse the gpg stuff properly. Cross fingers
> + my ($a) = Mail::Address->parse($uid->as_string); # list
> context, please
> + $key_cache{$a->address}=1 if ref $a;
> + }
> }
> }
>
> diff --git a/t/35.has-public-key.t b/t/35.has-public-key.t
> new file mode 100644
> index 0000000..2fbb9ab
> --- /dev/null
> +++ b/t/35.has-public-key.t
> @@ -0,0 +1,21 @@
> +# -*- perl -*-
> +
> +use Test::More;
> +use Mail::GnuPG;
> +use strict;
> +
> +require('t/import_keys.pl');
> +my $gpghome=import_keys('t/test-key.pgp');
> +unless (defined($gpghome)){
> + plan skip_all => "failed to import GPG keys for testing";
> + goto end;
> +}
> +
> +plan tests => 2;
> +
> +my $mg = new Mail::GnuPG( keydir => $gpghome );
> +isa_ok($mg, 'Mail::GnuPG');
> +
> +ok($mg->has_public_key('mail@gnupg.dom'), 'has_public_key works');
> +
> +end: