summaryrefslogtreecommitdiff
path: root/passmenu
blob: 9c833249e66685791610e9e4ec4886e5861fdcd8 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/perl

# Copyright (c) 2020, Bertalan Zoltán Péter <bp99@bp99.eu>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use 5.30.1;
use strict;
use warnings;
use diagnostics;
use experimental qw(smartmatch);
use Clipboard;
use File::Find qw(find);
use Getopt::Long;
use Pod::Usage qw(pod2usage);
use constant VERSION => 'passmenu v0.1';


my ($gui, $dmenu, $login, $print, $help, $man, $version) = (0) x 7;
# Getopt::Long::Configure('auto_version');
GetOptions('gui'     => \$gui,
           'dmenu'   => \$dmenu,
           'login'   => \$login,
           'print'   => \$print,
           'help'    => \$help,
           'man'     => \$man,
           'version' => \$version) or pod2usage 2;

if ($version) { say VERSION; exit; }
pod2usage -verbose => 1 if $help;
pod2usage -verbose => 2 if $man;


my $cli = -t STDIN ? ($gui ? 0 : 1) : 0;

! system 'pass version >/dev/null 2>&1' or die "I couldn't find password-store";

my $menu;
if (not $cli) {
    my $dmenu_ = ! system 'dmenu -v >/dev/null 2>&1';
    my $rofi_  = ! system 'rofi -v >/dev/null 2>&1';
    die "I couldn't find either rofi or dmenu" if not $dmenu_ and not $rofi_;
    die "I coudln't find dmenu" if $dmenu and not $dmenu_;
    $menu = $dmenu ? 'dmenu' : ($rofi_ ? 'rofi' : 'dmenu');
} else {
    my $fzf_ = ! system 'fzf --version >/dev/null 2>&1';
    $menu = $fzf_ ? 'fzf' : 'internal';
}

my $dir = "$ENV{HOME}/.password-store/";
my @files;
find sub {
    return if substr($_, -4, 4) ne '.gpg';
    push @files, $File::Find::name; }, $dir;
@files = map { (my $f = $_ ) =~ s/^$dir//; $f =~ s/\.gpg//; $f } @files;

my $choice;
if ($menu eq 'internal') {
    print 'query: ';
    my $query = <STDIN>;
    chomp $query;

    my @result = grep { /$query/ } @files;
    say $_ + 1, "\t", $result[$_] for (0 .. $#result);

    my $n;
    do {
        print 'selection: ';
        $n = <STDIN>;
        chomp $n;
    } while ! $n || ! grep { /$n/ } (0 .. $#result);

    $choice = $result[$n - 1];
} else {
    my $filelist = join "\\n", @files;
    if ($menu eq 'dmenu') {
        $choice = `echo "$filelist" | dmenu -i`;
    } elsif ($menu eq 'rofi') {
        $choice = `echo "$filelist" | rofi -dmenu -p pass -i`;
    } elsif ($menu eq 'fzf') {
        $choice = `echo "$filelist" | fzf --height=8`;
    }
}

chomp $choice;
my $pass = `pass show '$choice'`;
if ($login) {
    $pass =~ s/.*login: (.+)\n.*/$1/s;
} else {
    $pass =~ s/\n.*//s;
}

if (index($pass, "\n") != -1) {
    if ($cli) {
        die 'Result continains newline, aborting';
    } else {
        system "xmessage -center -default okay 'Result continains newline, aborting'";
    }
}
Clipboard->copy($pass);


__END__

=encoding utf8
=head1 NAME

passmenu - Handy password-store frontend

=head1 SYNOPSIS

passmenu [options]

 Options:
   -g, --gui            force dmenu or rofi, even if called from terminal
   -d, --dmenu          force dmenu even if rofi exists
   -l, --login          copy the username, not the password
   -p, --print          instead of copying, print the value
   -h, --help           display this help message
   -m, --man            open the manual
   -v, --version        print programme version

=head1 DESCRIPTION

This programme is designed to ease the use of password-store (or pass
for short).

When you are in a terminal, passmenu will prompt you for a query string
either using fzf if you have it, or using a less fancy internal method.

When called from anywhere else (e.g. launched by dmenu or by your
window manager), it will display a prompt either using dmenu or,
preferably, rofi.

Therefore you must have either rofi or dmenu to use passmenu outside a
terminal.

Once you select a pass entry using the menu, it will be copied to the
clipboard for you to do whatever you want with it.

=head1 AUTHOR

Bertalan Z. Péter <bp99@bp99.eu>

=cut