Hello Daniel,

On Thu 21 Nov 2019 at 02:30PM +08, Daniel Kahn Gillmor wrote:

> diff --git a/email-print-mime-structure b/email-print-mime-structure
> index 27fb532..ab90976 100755
> --- a/email-print-mime-structure
> +++ b/email-print-mime-structure
> @@ -81,6 +81,7 @@ class MimePrinter(object):
>          cryptopayload:Optional[Message] = None
>          ciphertext:Union[List[Message],str,bytes,None] = None
>          try_pgp_decrypt:bool = self.args.pgpkey or self.args.use_gpg_agent
> +        try_cms_decrypt:bool = self.args.use_gpg_agent
>
>          if try_pgp_decrypt and \
>             (parent is not None) and \
> @@ -99,6 +100,19 @@ class MimePrinter(object):
>                  logging.warning(f'Unable to decrypt')
>                  return
>
> +        if try_cms_decrypt and \
> +           cryptopayload is None and \
> +           z.get_content_type().lower() == 'application/pkcs7-mime':
> +            ciphertext = z.get_payload(decode=True)
> +            if not isinstance(ciphertext, bytes):
> +                logging.warning('encrypted part was not a leaf mime part 
> somehow')
> +                return
> +            if self.args.use_gpg_agent:
> +                cryptopayload = self.gpgsm_decrypt(ciphertext)

I think self.args.use_gpg_agent will always be True at this point in the
control flow?

> +    def gpgsm_decrypt(self, ciphertext:bytes) -> Optional[Message]:
> +        inp:int
> +        outp:int
> +        inp, outp = os.pipe()
> +        with open(outp, 'wb') as outf:
> +            outf.write(ciphertext)
> +        try:
> +            out:subprocess.CompletedProcess[bytes] = 
> subprocess.run(['gpgsm', '--batch', '--decrypt'],
> +                                                                    
> stdin=inp,
> +                                                                    
> capture_output=True)
> +        except Exception as e:
> +            logging.warning(f'Failed to decrypt with gpgsm: {e}')
> +            return None
> +        if out.returncode == 0:
> +            return email.message_from_bytes(out.stdout)
> +        return None
> +

This function is almost identical to gpg_decrypt.  Please refactor.

-- 
Sean Whitton

Attachment: signature.asc
Description: PGP signature

Reply via email to