From 92cb89ad33083a917710cafe057d4303c8bc91eb Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <adrien.waksberg@doctolib.fr>
Date: Sun, 2 Apr 2017 12:14:27 +0200
Subject: [PATCH 1/7] fix gpg password with pinentry

---
 lib/mpw/mpw.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb
index 8389be9..2444f12 100644
--- a/lib/mpw/mpw.rb
+++ b/lib/mpw/mpw.rb
@@ -339,7 +339,7 @@ module MPW
 
       crypto = GPGME::Crypto.new(armor: true)
 
-      crypto.decrypt(data, password: @gpg_pass).read.force_encoding('utf-8')
+      crypto.decrypt(data, password: @gpg_pass, pinentry_mode: GPGME::PINENTRY_MODE_LOOPBACK).read.force_encoding('utf-8')
     rescue => e
       raise "#{I18n.t('error.gpg_file.decrypt')}\n#{e}"
     end

From d1adfd24c10ab9a4fd80cb3c08ebbfbc069bc585 Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <git@yae.im>
Date: Sun, 2 Apr 2017 23:44:36 +0200
Subject: [PATCH 2/7] fix pinentry mode with gpg 1.4

---
 lib/mpw/mpw.rb | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb
index 2444f12..88cab5c 100644
--- a/lib/mpw/mpw.rb
+++ b/lib/mpw/mpw.rb
@@ -337,9 +337,18 @@ module MPW
     def decrypt(data)
       return nil if data.to_s.empty?
 
-      crypto = GPGME::Crypto.new(armor: true)
+      password =
+        if /^1\.[0-9.]+$/ =~ GPGME::Engine.info.first.version
+          { password: @gpg_pass }
+        else
+          { password: @gpg_pass,
+            pinentry_mode: GPGME::PINENTRY_MODE_LOOPBACK }
+        end
 
-      crypto.decrypt(data, password: @gpg_pass, pinentry_mode: GPGME::PINENTRY_MODE_LOOPBACK).read.force_encoding('utf-8')
+      crypto = GPGME::Crypto.new(armor: true)
+      crypto
+        .decrypt(data, password)
+        .read.force_encoding('utf-8')
     rescue => e
       raise "#{I18n.t('error.gpg_file.decrypt')}\n#{e}"
     end

From 7ce4ba721bb039f30af6882dca9c55e748b58944 Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <git@yae.im>
Date: Tue, 4 Apr 2017 23:45:03 +0200
Subject: [PATCH 3/7] add option to enable pinmode with gpg >= 2.1

---
 bin/mpw-config    | 6 +++++-
 lib/mpw/cli.rb    | 2 +-
 lib/mpw/config.rb | 6 +++++-
 lib/mpw/mpw.rb    | 5 +++--
 4 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/bin/mpw-config b/bin/mpw-config
index 4740ed1..1e3480d 100644
--- a/bin/mpw-config
+++ b/bin/mpw-config
@@ -60,6 +60,10 @@ OptionParser.new do |opts|
     values[:lang] = lang
   end
 
+  opts.on('-P', '--pinmode', I18n.t('option.pinmode')) do
+    values[:pinmode] = true
+  end
+
   opts.on('-w', '--wallet-dir PATH', I18n.t('option.wallet_dir')) do |wallet_dir|
     values[:wallet_dir] = wallet_dir
   end
@@ -94,7 +98,7 @@ OptionParser.new do |opts|
 end.parse!
 
 config = MPW::Config.new(options[:config])
-cli    = MPW::Cli.new(config, nil)
+cli    = MPW::Cli.new(config)
 
 if options.key?(:init)
   cli.setup(values)
diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb
index d4d4027..56e77a1 100644
--- a/lib/mpw/cli.rb
+++ b/lib/mpw/cli.rb
@@ -101,7 +101,7 @@ module MPW
     def decrypt
       unless defined?(@mpw)
         @password = ask(I18n.t('display.gpg_password')) { |q| q.echo = false }
-        @mpw      = MPW.new(@config.gpg_key, @wallet_file, @password, @config.gpg_exe)
+        @mpw      = MPW.new(@config.gpg_key, @wallet_file, @password, @config.gpg_exe, @config.pinmode)
       end
 
       @mpw.read_data
diff --git a/lib/mpw/config.rb b/lib/mpw/config.rb
index 3b88b0a..829abc2 100644
--- a/lib/mpw/config.rb
+++ b/lib/mpw/config.rb
@@ -32,6 +32,7 @@ module MPW
     attr_accessor :wallet_dir
     attr_accessor :gpg_exe
     attr_accessor :password
+    attr_accessor :pinmode
 
     # Constructor
     # @args: config_file -> the specify config file
@@ -59,6 +60,7 @@ module MPW
       wallet_dir     = options[:wallet_dir]     || @wallet_dir
       default_wallet = options[:default_wallet] || @default_wallet
       gpg_exe        = options[:gpg_exe]        || @gpg_exe
+      pinmode        = options[:pinmode]        || @pinmode
       password       = { numeric: true,
                          alpha:   true,
                          special: false,
@@ -82,7 +84,8 @@ module MPW
                      'wallet_dir'     => wallet_dir,
                      'default_wallet' => default_wallet,
                      'gpg_exe'        => gpg_exe,
-                     'password'       => password }
+                     'password'       => password,
+                     'pinmode'        => pinmode }
 
       FileUtils.mkdir_p(@config_dir, mode: 0700)
       FileUtils.mkdir_p(wallet_dir,  mode: 0700)
@@ -132,6 +135,7 @@ module MPW
       @default_wallet = config['default_wallet']
       @gpg_exe        = config['gpg_exe']
       @password       = config['password'] || {}
+      @pinmode        = config['pinmode']
 
       raise if @gpg_key.empty? || @wallet_dir.empty?
 
diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb
index 88cab5c..7d15b52 100644
--- a/lib/mpw/mpw.rb
+++ b/lib/mpw/mpw.rb
@@ -26,11 +26,12 @@ require 'mpw/item'
 module MPW
   class MPW
     # Constructor
-    def initialize(key, wallet_file, gpg_pass = nil, gpg_exe = nil)
+    def initialize(key, wallet_file, gpg_pass = nil, gpg_exe = nil, pinmode = false)
       @key         = key
       @gpg_pass    = gpg_pass
       @gpg_exe     = gpg_exe
       @wallet_file = wallet_file
+      @pinmode     = pinmode
 
       GPGME::Engine.set_info(GPGME::PROTOCOL_OpenPGP, @gpg_exe, "#{Dir.home}/.gnupg") unless @gpg_exe.to_s.empty?
     end
@@ -338,7 +339,7 @@ module MPW
       return nil if data.to_s.empty?
 
       password =
-        if /^1\.[0-9.]+$/ =~ GPGME::Engine.info.first.version
+        if /^1\.[0-9.]+$/ =~ GPGME::Engine.info.first.version || @pinmode
           { password: @gpg_pass }
         else
           { password: @gpg_pass,

From d8df357993212cc6d43148eaeb3f907f7479fa5c Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <git@yae.im>
Date: Tue, 4 Apr 2017 23:50:38 +0200
Subject: [PATCH 4/7] not use pinmode if gpg version < 2.1

---
 lib/mpw/mpw.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb
index 7d15b52..dd12ef6 100644
--- a/lib/mpw/mpw.rb
+++ b/lib/mpw/mpw.rb
@@ -339,7 +339,7 @@ module MPW
       return nil if data.to_s.empty?
 
       password =
-        if /^1\.[0-9.]+$/ =~ GPGME::Engine.info.first.version || @pinmode
+        if /^(1\.[0-9.]+|2\.0)(\.[0-9]+)?/ =~ GPGME::Engine.info.first.version || @pinmode
           { password: @gpg_pass }
         else
           { password: @gpg_pass,

From 45ead1e24e13fda0161dab564198980ae2b57abd Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <git@yae.im>
Date: Tue, 4 Apr 2017 23:52:00 +0200
Subject: [PATCH 5/7] fix syntax for rubocop 0.48.1

---
 lib/mpw/config.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/mpw/config.rb b/lib/mpw/config.rb
index 829abc2..591b991 100644
--- a/lib/mpw/config.rb
+++ b/lib/mpw/config.rb
@@ -66,7 +66,7 @@ module MPW
                          special: false,
                          length:  16 }
 
-      %w(numeric special alpha length).each do |k|
+      %w[numeric special alpha length].each do |k|
         if options.key?("pwd_#{k}".to_sym)
           password[k.to_sym] = options["pwd_#{k}".to_sym]
         elsif !@password.nil? && @password.key?(k.to_sym)

From 621819203f4025c9632f79971383c76b8c0415bf Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <git@yae.im>
Date: Wed, 5 Apr 2017 00:03:53 +0200
Subject: [PATCH 6/7] add option to disable pinmode

---
 bin/mpw-config | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/bin/mpw-config b/bin/mpw-config
index 1e3480d..67ffc50 100644
--- a/bin/mpw-config
+++ b/bin/mpw-config
@@ -60,10 +60,14 @@ OptionParser.new do |opts|
     values[:lang] = lang
   end
 
-  opts.on('-P', '--pinmode', I18n.t('option.pinmode')) do
+  opts.on('-P', '--enable-pinmode', I18n.t('option.pinmode')) do
     values[:pinmode] = true
   end
 
+  opts.on('-p', '--disable-pinmode', I18n.t('option.disable_pinmode')) do
+    values[:pinmode] = false
+  end
+
   opts.on('-w', '--wallet-dir PATH', I18n.t('option.wallet_dir')) do |wallet_dir|
     values[:wallet_dir] = wallet_dir
   end

From 3b5bb48e6ba6cdcc9ae60783a62a85440e406761 Mon Sep 17 00:00:00 2001
From: Adrien Waksberg <git@yae.im>
Date: Wed, 5 Apr 2017 00:04:15 +0200
Subject: [PATCH 7/7] add translate for pinmode options

---
 i18n/en.yml | 2 ++
 i18n/fr.yml | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/i18n/en.yml b/i18n/en.yml
index 162f6b3..97f1928 100644
--- a/i18n/en.yml
+++ b/i18n/en.yml
@@ -49,6 +49,7 @@ en:
     delete_gpg_key: "Delete the wallet's share with an other GPG key"
     disable_alpha: "Don't use letter to generate a password"
     disable_numeric: "Don't use number to generate a password"
+    disable_pinmode: "Disable the pinentry mode"
     disable_special_chars: "Don't use special char to generate a password"
     export: "Export a wallet in an yaml file"
     file_export: "Specify the file where export data"
@@ -68,6 +69,7 @@ en:
     list_keys: "List the GPG keys in wallet"
     numeric: "Use number to generate a password"
     pattern: "Given search pattern"
+    pinmode: "Enable pinentry mode (available with gpg >= 2.1)"
     random_password: "Generate a random password"
     setup: "Create a new configuration file" 
     setup_wallet: "Create a new configuration file for a wallet"
diff --git a/i18n/fr.yml b/i18n/fr.yml
index b2e3db4..0c7319c 100644
--- a/i18n/fr.yml
+++ b/i18n/fr.yml
@@ -49,6 +49,7 @@ fr:
     delete_gpg_key: "Supprime le partage le portefeuille avec une autre clé GPG"
     disable_alpha: "Désactive l'utilisation des lettres dans la génération d'un mot de passe"
     disable_numeric: "Désactive l'utilisation des chiffre dans la génération d'un mot de passe"
+    disable_pinmode: "Désactive le mode pinentry"
     disable_special_chars: "Désactive l'utilisation des charactères speciaux dans la génération d'un mot de passe"
     export: "Exporte un portefeuille dans un fichier yaml"
     file_export: "Spécifie le fichier où exporter les données"
@@ -68,6 +69,7 @@ fr:
     list_keys: "Liste les clés GPG dans le portefeuille"
     numeric: "Utilise des chiffre dans la génération d'un mot de passe"
     pattern: "Motif de donnée à chercher"
+    pinmode: "Active le mode pinentry (valable avec gpg >= 2.1)"
     random_password: "Génére un mot de passe aléatoire"
     setup: "Création d'un nouveau fichier de configuration"
     setup_wallet: "Création d'un nouveau fichier de configuration pour un portefeuille"