]> git.madduck.net Git - etc/zsh.git/blob - .zsh/func/getpw

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

108b4cdfcc4ab825c76224d2c71ca2630db9c1ec
[etc/zsh.git] / .zsh / func / getpw
1 #!/bin/sh
2 #
3 # func/getpw
4 #
5 # obtain a password from ~/pw.gpg
6 #
7 # Copyright © 2011 martin f. krafft <madduck@madduck.net>
8 # Released under the terms of the Artistic Licence 2.0
9 #
10 # Source repository: http://git.madduck.net/v/etc/zsh.git
11 #
12
13 local PWENCFILE=${HOME}/pw.gpg
14 local incother=0
15 local dest clip=1
16 [ -x $(command -v xclip) ] || clip=0
17
18 OPTIND=0
19 while getopts :po opt; do
20   case "$opt" in
21     \?) echo >&2 "E: unrecognised option: -$OPTARG"; return 1;;
22      p) clip=0;;
23      o) incother=1;;
24   esac
25 done
26 (( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
27
28 if [[ $clip = 1 ]]; then
29   dest='tr -d \\n | xclip -in'
30 else
31   dest="column -ts'     '"
32 fi
33
34 if (( $# == 1 )); then
35   case "$1" in
36     *@*) local identity="${1%@*}"
37          local resource="${1##*@}"
38          ;;
39     *) local resource="$1";;
40   esac
41 else
42   local resource="$1"
43   local identity="$2"
44 fi
45
46 local IFSOLD="$IFS"; IFS="      "
47 local output
48 gpg --decrypt --quiet "$PWENCFILE" | \
49   {
50     output=$(while read r i p o; do
51       case "$r:l/$i:l" in
52         (*${resource:l}*/${identity:+*${identity:l}}*)
53           echo "'$r'    '$i'    '$p'    '$o'";;
54       esac
55     done)
56   }
57 [ -z "$output" ] && return 0
58
59 typeset -al results
60 results=(${(f)output})
61
62 typeset -al result
63 if (( ${#results} == 1 )); then
64   result=(${(z)${results[1]}})
65   output="${result[3]}"
66   [[ $incother = 1 ]] && output="$output ${result[4]}"
67   eval echo "$output" | eval $dest
68   [[ $clip = 1 ]] && eval echo >&2 "match for ${result[1]}, ID ${result[2]} put onto X clipboard."
69 else
70   if [[ $clip = 1 ]]; then
71     echo >&2 "E: multiple matches, hence not putting onto clipboard; use -p option."
72     return 1
73   fi
74   for r in $results; do
75     result=(${(z)r})
76     output="${result[2]}@${result[1]}   ${result[3]}"
77     [[ $incother = 1 ]] && output="$output ${result[4]}"
78     echo "$output"
79   done | eval $dest
80 fi
81
82 IFS="$IFSOLD"