mhsign: Fix key lookup of aliases when encrypting
[mmh] / uip / mhsign.sh
index ac56bc2..17b7489 100755 (executable)
@@ -18,12 +18,19 @@ function=sign
 
 # find out the signing key
 userid="$MMHPGPKEY"
-if [ "x$userid" = "x" ] ; then
+if [ -z "$userid" ] ; then
        userid="`mhparam pgpkey`"
 fi
-userid="`gpg --list-secret-keys --with-colons 2>/dev/null |
-               sed -n '/^sec/{p;q}' | cut -d: -f5`"
-if [ "x$userid" = x ] ; then
+if [ -z "$userid" ] ; then
+       userid="`gpg --list-secret-keys --with-colons 2>/dev/null |
+                       grep '^sec' | sort -t: -k3,3nr -k 6,6nr |
+                       awk -F: '
+                               $7=="" || $7 > "'"\`date +%Y-%m-%d\`"'" {
+                                       print $5; exit;
+                               }
+                       '`"
+fi
+if [ -z "$userid" ] ; then
        echo "No secret key found" >&2
        exit 1
 fi
@@ -89,7 +96,7 @@ lookupkeyring() {
        if [ $? != 0 ] ; then
                return 1
        fi
-       echo "$key" | sed -n '/^pub/{p;q}' | cut -d: -f5
+       echo "$key" | sed -n '/^pub:[^idre]:/{p;q;}' | cut -d: -f5
        return 0
 }
 
@@ -101,14 +108,17 @@ lookupkeys() {
                echo "Encryption is not supported for BCCs" >&2
                return 1
        fi
-               
-       for i in `whom -ali -tocc -nobcc "$1"` ; do
+
+       whom -ali -tocc -nobcc "$1" | while read i ; do
                case "$i" in
                '|'*)   echo "Ignoring pipe address" >&2
                        continue ;;
                *@*)    ;;
-               *)      i="$i@`hostname -f`" ;;
+               *)      a="$i@`hostname -f`" ;;
                esac
+               # extract the actual address
+               format='%<{error}%{error}: %{text}%|%(addr{text})%>'
+               i=`%libdir%/ap -form "=$format" "$i"`
                if k=`lookupkeyfile "$i"` ; then
                        KL="$KL $k"
                elif k=`lookupkeyring "$i"` ; then