]> git.donarmstrong.com Git - zsh.git/commitdiff
initial checking of getpw
authormartin f. krafft <madduck@madduck.net>
Mon, 14 Mar 2011 18:58:24 +0000 (19:58 +0100)
committermartin f. krafft <madduck@madduck.net>
Mon, 14 Mar 2011 18:58:24 +0000 (19:58 +0100)
.zsh/func/getpw [new file with mode: 0755]

diff --git a/.zsh/func/getpw b/.zsh/func/getpw
new file mode 100755 (executable)
index 0000000..7d93e42
--- /dev/null
@@ -0,0 +1,82 @@
+#!/bin/sh
+#
+# func/getpw
+#
+# obtain a password from ~/pw.gpg
+#
+# Copyright © 2011 martin f. krafft <madduck@madduck.net>
+# Released under the terms of the Artistic Licence 2.0
+#
+# Source repository: git://git.madduck.net/etc/zsh.git
+#
+
+local PWENCFILE=${HOME}/pw.gpg
+local incother=0
+local dest clip=1
+[ -x $(command -v xclip) ] || clip=0
+
+OPTIND=0
+while getopts :po opt; do
+  case "$opt" in
+    \?) echo >&2 "E: unrecognised option: -$OPTARG"; return 1;;
+     p) clip=0;;
+     o) incother=1;;
+  esac
+done
+(( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
+
+if [[ $clip = 1 ]]; then
+  dest='tr -d \\n | xclip -in'
+else
+  dest="column -ts'    '"
+fi
+
+if (( $# == 1 )); then
+  case "$1" in
+    *@*) local identity="${1%@*}"
+         local resource="${1##*@}"
+         ;;
+    *) local resource="$1";;
+  esac
+else
+  local resource="$1"
+  local identity="$2"
+fi
+
+local IFSOLD="$IFS"; IFS="     "
+local output
+gpg --decrypt --batch --quiet "$PWENCFILE" | \
+  {
+    output=$(while read r i p o; do
+      case "$r/$i" in
+        (*${resource}*/${identity:+*${identity}}*)
+          echo "$r     $i      $p      $o";;
+      esac
+    done)
+  }
+[ -z "$output" ] && return 0
+
+typeset -al results
+results=(${(f)output})
+
+typeset -al result
+if (( ${#results} == 1 )); then
+  result=(${(z)${results[1]}})
+  output="${result[3]}"
+  [[ $incother = 1 ]] && output="$output ${result[4]}"
+  echo "$output" | eval $dest
+  [[ $clip = 1 ]] && echo >&2 "match for ${result[2]}@${result[1]} put onto X clipboard."
+else
+  if [[ $clip = 1 ]]; then
+    echo >&2 "E: multiple matches, hence not putting onto clipboard; use -p option."
+    return 1
+  fi
+  for r in $results; do
+    result=(${(z)r})
+    output="${result[2]}       ${result[3]}"
+    [[ $incother = 1 ]] && output="$output ${result[4]}"
+    echo "$output"
+  done | eval $dest
+fi
+
+IFS="$IFSOLD"