summary refs log tree commit diff
path: root/bin/pasm
blob: 60c0394347669687b34210e8a555acabbe9177da (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
#!/bin/sh
# local is well supported
# shellcheck disable=3043
set -eux

# shellcheck disable=2034
description="rbw-based password manager"

FUZL_PROMPT=pasm
FUZL_ICON=󰈲
. libfuzl.sh

main() {
	rbw unlocked 2>/dev/null ||
		rbw unlock || exit 1

	local eid
	eid=$(rbw_select_entry)
	case "$eid" in
	cards)
		eid=$(rbw list --raw |
			jq -r '.[]|select(.type=="Card")|"\(.id)\t\(.name)"' |
			fuzl_menu $FUZL_MENU_FILTER --with-nth=2 --accept-nth=1)

		fuzl_action_add "raw" "󱃖" "view json"
		fuzl_action_menu "$eid" || exit 1
		;;
	identities)
		eid=$(rbw list --raw |
			jq -r '.[]|select(.type=="Identity")|"\(.id)\t\(.name)"' |
			fuzl_menu $FUZL_MENU_FILTER --with-nth=2 --accept-nth=1)

		fuzl_action_add "raw" "󱃖" "view json"
		fuzl_action_menu "$eid" || exit 1
		;;
	*)
		fuzl_action_add "copy" "󰀄" "copy username/password"
		rbw_has_totp "$eid" && fuzl_action_add "totp" "󰎃" "copy totp code"
		fuzl_action_add "url" "󰌹" "copy url"
		fuzl_action_add "raw" "󱃖" "view json"

		fuzl_action_menu "$eid" || exit 1
		;;
	esac

}

#
# helpers
#

rbw_select_entry() {
	(
		rbw list --raw |
			jq -r '.[] | select(.type=="Login") | [.id,.name,.user,(.uris[0]//"-")]|join("%")' |
			awk -F% '{print $1 "\t" ($2 " →") "\t" ("󰀄 " $3) "\t" ("󰌹 " $4)}'
		printf 'cards\t~> C\tcards\n'
		printf 'identities\t~> I\tidentities\n'
		printf 'keys\t~> K\tkeys\n'
		printf 'notes\t~> N\tnotes\n'
	) |
		fuzl_menu $FUZL_MENU_FILTER --with-nth='{2}       {3..}' --accept-nth=1 -w 80
}

rbw_has_totp() {
	local eid=${1:?missing eid}
	# can't use fuzl_try here because we need the exit status
	rbw get -l "$eid" | grep -q totp
}
rbw_entry() {
	local eid=${1:?missing eid}
	fuzl_try -- rbw get --raw "$eid" | jq -r '.name'
}

#
# actions
#

pasm_copy() {
	local eid=${1:?missing eid}
	fuzl_try -- rbw get -f password "$eid" | wl-copy -n
	fuzl_try -- rbw get -f username "$eid" | wl-copy -p -n
	fuzl_notification "$(rbw_entry "$eid")" "copied to clipboard"
}

pasm_totp() {
	local eid=${1:?missing eid}
	fuzl_try -- rbw totp "$eid" | wl-copy -n
	fuzl_notification "$(rbw_entry "$eid")" "copied totp to clipboard"
}

pasm_url() {
	local eid=${1:?missing eid}
	fuzl_try -- rbw get -f uris "$eid" | head -n1 | wl-copy -n
	fuzl_notification "$(rbw_entry "$eid")" "copied url to clipboard"
}

pasm_raw() {
	local eid=${1:?missing eid}
	local tmpfile="/tmp/$(printf '%s' "$eid" | sha1sum | cut -d\  -f1).json"
	fuzl_try -- rbw get --raw "$eid" >"$tmpfile"
	fuzl_try -- urlopen open "$tmpfile"
}

main