From e27595381323ca8b95446a6ec25f0ef7e2480700 Mon Sep 17 00:00:00 2001
From: "J. Nathanael Philipp" <nathanael@philipp.land>
Date: Fri, 13 Sep 2019 00:14:15 +0200
Subject: [PATCH 1/4] Fix result icon.

---
 gnome-pass-search-provider.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gnome-pass-search-provider.py b/gnome-pass-search-provider.py
index 5b1d0ff..39a844d 100755
--- a/gnome-pass-search-provider.py
+++ b/gnome-pass-search-provider.py
@@ -72,7 +72,7 @@ class SearchPassService(dbus.service.Object):
 
     @dbus.service.method(in_signature='as', out_signature='aa{sv}', **sbn)
     def GetResultMetas(self, ids):
-        return [dict(id=id, name=id, gicon="password-manager") for id in ids]
+        return [dict(id=id, name=id, gicon='dialog-password') for id in ids]
 
     @dbus.service.method(in_signature='asas', out_signature='as', **sbn)
     def GetSubsearchResultSet(self, previous_results, new_terms):

From 706bee9e36921392c6c2aeebb38e0d643491a2a2 Mon Sep 17 00:00:00 2001
From: "J. Nathanael Philipp" <nathanael@philipp.land>
Date: Fri, 13 Sep 2019 00:17:19 +0200
Subject: [PATCH 2/4] Add option to copy other field values.

---
 gnome-pass-search-provider.py | 72 +++++++++++++++++++++++------------
 1 file changed, 48 insertions(+), 24 deletions(-)

diff --git a/gnome-pass-search-provider.py b/gnome-pass-search-provider.py
index 39a844d..aa83ec3 100755
--- a/gnome-pass-search-provider.py
+++ b/gnome-pass-search-provider.py
@@ -26,17 +26,15 @@
 # Author: Luke Macken <lmacken@redhat.com>
 
 from fuzzywuzzy import process, fuzz
-from os import getenv
-from os import walk
-from os.path import expanduser
-from os.path import join as path_join
-import re
-import subprocess
+from gi.repository import GLib
+from os import getenv, walk
+from os.path import expanduser, join as path_join
 
 import dbus
 import dbus.glib
 import dbus.service
-from gi.repository import GLib
+import re
+import subprocess
 
 # Convenience shorthand for declaring dbus interface methods.
 # s.b.n. -> search_bus_name
@@ -52,7 +50,6 @@ class SearchPassService(dbus.service.Object):
 
     """
     bus_name = 'org.gnome.Pass.SearchProvider'
-
     _object_path = '/' + bus_name.replace('.', '/')
 
     def __init__(self):
@@ -72,7 +69,8 @@ class SearchPassService(dbus.service.Object):
 
     @dbus.service.method(in_signature='as', out_signature='aa{sv}', **sbn)
     def GetResultMetas(self, ids):
-        return [dict(id=id, name=id, gicon='dialog-password') for id in ids]
+        return [dict(id=id, name=id[1:] if id.startswith(':') else id,
+                     gicon='dialog-password') for id in ids]
 
     @dbus.service.method(in_signature='asas', out_signature='as', **sbn)
     def GetSubsearchResultSet(self, previous_results, new_terms):
@@ -84,14 +82,15 @@ class SearchPassService(dbus.service.Object):
 
     def get_result_set(self, terms):
         if terms[0] == 'otp':
-            otp = True
+            field = terms[0]
+        elif terms[0].startswith(':'):
+            field = terms[0][1:]
             terms = terms[1:]
         else:
-            otp = False
+            field = None
 
         name = ''.join(terms)
         password_list = []
-
         for root, dirs, files in walk(self.password_store):
             dir_path = root[len(self.password_store) + 1:]
 
@@ -106,16 +105,28 @@ class SearchPassService(dbus.service.Object):
 
         results = [e[0] for e in process.extract(name, password_list, limit=5,
                                                  scorer=fuzz.partial_ratio)]
-        if otp:
+        if field == 'otp':
             results = [f'otp {r}' for r in results]
+        elif field is not None:
+            results = [f':{field} {r}' for r in results]
         return results
 
-    def send_password_to_gpaste(self, base_args, name):
+    def send_password_to_gpaste(self, base_args, name, field=None):
         try:
-            pass_output = subprocess.check_output(base_args + [name],
-                                                  stderr=subprocess.STDOUT,
-                                                  universal_newlines=True)
-            password = pass_output.split('\n', 1)[0]
+            output = subprocess.check_output(base_args + [name],
+                                             stderr=subprocess.STDOUT,
+                                             universal_newlines=True)
+            if field is not None:
+                match = re.search(fr'^{field}:\s*(?P<value>.+?)$', output,
+                                  flags=re.I|re.M)
+                if match:
+                    password = match.group('value')
+                else:
+                    raise RuntimeError(f'The field {field} not found in pass' +
+                                       ' file.')
+            else:
+                password = output.split('\n', 1)[0]
+
             self.session_bus.get_object(
                 'org.gnome.GPaste.Daemon',
                 '/org/gnome/GPaste'
@@ -128,14 +139,23 @@ class SearchPassService(dbus.service.Object):
             if 'otp' in base_args:
                 self.notify('Copied OTP password to clipboard:',
                             body=f'<b>{name}</b>')
+            elif field is not None:
+                self.notify(f'Copied field {field} to clipboard:',
+                            body=f'<b>{name}</b>')
             else:
                 self.notify('Copied password to clipboard:',
                             body=f'<b>{name}</b>')
-        except subprocess.CalledProcessError as error:
-            self.notify('Failed to copy password!', body=error.output,
-                        error=True)
+        except subprocess.CalledProcessError as e:
+            self.notify('Failed to copy password!', body=e.output, error=True)
+        except RuntimeError as e:
+            self.notify('Failed to copy field!', body=e.output, error=True)
+
+    def send_password_to_native_clipboard(self, base_args, name, field=None):
+        if field is not None:
+            self.notify(f'Cannot copy field values.',
+                        body='This feature requires GPaste.', error=True)
+            return
 
-    def send_password_to_native_clipboard(self, base_args, name):
         pass_cmd = subprocess.run(base_args + ['-c', name])
         if pass_cmd.returncode:
             self.notify('Failed to copy password!', error=True)
@@ -146,18 +166,22 @@ class SearchPassService(dbus.service.Object):
             self.notify('Copied password to clipboard:', body=f'<b>{name}</b>')
 
     def send_password_to_clipboard(self, name):
+        field = None
         if name.startswith('otp '):
             base_args = ['pass', 'otp', 'code']
             name = name[4:]
         else:
             base_args = ['pass', 'show']
+            if name.startswith(':'):
+                field, name = name.split(' ', 1)
+                field = field[1:]
 
         try:
-            self.send_password_to_gpaste(base_args, name)
+            self.send_password_to_gpaste(base_args, name, field)
         except dbus.DBusException:
             # We couldn't join GPaste over D-Bus,
             # use pass native clipboard copy
-            self.send_password_to_native_clipboard(base_args, name)
+            self.send_password_to_native_clipboard(base_args, name, field)
 
     def notify(self, message, body='', error=False):
         try:

From 1e53479189916be3523262c309177b05d880a399 Mon Sep 17 00:00:00 2001
From: "J. Nathanael Philipp" <nathanael@philipp.land>
Date: Fri, 13 Sep 2019 13:14:29 +0200
Subject: [PATCH 3/4] Update README.

---
 README.md | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/README.md b/README.md
index d6291c2..ce7d4f2 100644
--- a/README.md
+++ b/README.md
@@ -41,6 +41,19 @@ The search provider should show up and be enabled in GNOME search preferences an
 
 The [pass-otp](https://github.com/tadfisher/pass-otp) extension is supported. Searches starting with `otp` will copy the otp token to the clipboard.
 
+# Fields
+
+To copy other values than the password in the first line from a pass file, start the search with `:NAME search...`. This requieres `GPaste`.
+
+For example with a pass file like:
+```
+SUPERSECRETPASSWORD
+user: username
+pin: 123456
+```
+
+To copy the pin start the search with `:pin` and for the username `:user`.
+
 # Environment variables
 
 If you are configuring pass through environment variables, such as `PASSWORD_STORE_DIR`, make sure to set them in a way that will propagate to the search provider executable, not just in your shell.

From 2ca2ad54c78716e2367e5754911113055391980d Mon Sep 17 00:00:00 2001
From: "J. Nathanael Philipp" <nathanael@philipp.land>
Date: Thu, 19 Sep 2019 14:59:18 +0200
Subject: [PATCH 4/4] Improvements.

---
 README.md                     |  2 +-
 gnome-pass-search-provider.py | 15 ++++++++-------
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md
index ce7d4f2..1bcce0d 100644
--- a/README.md
+++ b/README.md
@@ -43,7 +43,7 @@ The [pass-otp](https://github.com/tadfisher/pass-otp) extension is supported. Se
 
 # Fields
 
-To copy other values than the password in the first line from a pass file, start the search with `:NAME search...`. This requieres `GPaste`.
+To copy other values than the password in the first line from a pass file, start the search with `:NAME search...`. The field name must be a full but case insensitive match. This requires `GPaste`.
 
 For example with a pass file like:
 ```
diff --git a/gnome-pass-search-provider.py b/gnome-pass-search-provider.py
index aa83ec3..b4a924d 100755
--- a/gnome-pass-search-provider.py
+++ b/gnome-pass-search-provider.py
@@ -25,17 +25,18 @@
 # Copyright (C) 2012 Red Hat, Inc.
 # Author: Luke Macken <lmacken@redhat.com>
 
-from fuzzywuzzy import process, fuzz
-from gi.repository import GLib
-from os import getenv, walk
-from os.path import expanduser, join as path_join
-
 import dbus
 import dbus.glib
 import dbus.service
 import re
 import subprocess
 
+from os import getenv, walk
+from os.path import expanduser, join as path_join
+
+from gi.repository import GLib
+from fuzzywuzzy import process, fuzz
+
 # Convenience shorthand for declaring dbus interface methods.
 # s.b.n. -> search_bus_name
 search_bus_name = 'org.gnome.Shell.SearchProvider2'
@@ -122,8 +123,8 @@ class SearchPassService(dbus.service.Object):
                 if match:
                     password = match.group('value')
                 else:
-                    raise RuntimeError(f'The field {field} not found in pass' +
-                                       ' file.')
+                    raise RuntimeError(f'The field {field} was not found in ' +
+                                       'the pass file.')
             else:
                 password = output.split('\n', 1)[0]