summaryrefslogtreecommitdiffstats
path: root/rofi-gpgpass
blob: 5193ee20110fa436b72cb2dece4679af00dbfc47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env ruby

#
# rofi-gpgpass - small script to type passwords from my passwords.gpg
#
# Copyright (c) 2022 Florian Pritz <bluewind@xinu.at>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http: #www.gnu.org/licenses/
#

require 'open3'

$pwfile = "#{ENV['HOME']}/passwords.gpg"

def get_passwords(pwfile)
  data = `gpg --quiet --batch --decrypt "#{pwfile}" | grep -v '^HIDEOLD'`
  raise "Failed to get passwords" if $? != 0 or data == '""'
  return data
end

def get_single_password(passwords, identifier)
  identifier.chomp!
  passwords.split("\n").each do |line|
    if line.end_with?(identifier)
      return line.gsub(/^"([^"]+)" .*$/, '\1').chomp if line =~ /^"/
      return line.gsub(/^([^ ]+) .*$/, '\1').chomp
    end
  end
  return nil
end

def get_identifiers(passwords)
  pass_array = passwords.split("\n").map do |line|
    next line.gsub(/^"([^"]+)" (.*)$/, '\2') if line =~ /^"/
    next line.gsub(/^([^ ]+) (.*)$/, '\2')
  end
  return pass_array.join("\n")
end

def rofi_select(passwords)
  Open3.popen2("rofi -lines 20 -dmenu -i -p password") do |stdin, stdout, wait_thr|
    stdin.puts get_identifiers(passwords)
    return stdout.gets
  end
end

def type_password(identifier, passwords)
  password = get_single_password(passwords, identifier)

  x_repeat_enabled = `xset q | awk '/auto repeat:/ {print $3}'`.chomp
  system("xset r off")

  Open3.popen2("xdotool type --clearmodifiers --file -") do |stdin, stdout, wait_thr|
    stdin.write password
  end

  system("xset r '#{x_repeat_enabled}'")
end

def main()
  passwords = get_passwords($pwfile)
  selection = rofi_select(passwords)
  type_password(selection, passwords)

  return 1
end


exit(main())