Skip to content

Instantly share code, notes, and snippets.

@mrvdb
Created February 8, 2018 10:44
Show Gist options
  • Select an option

  • Save mrvdb/77e1f9465bdcbc4aaa859537267ccf9f to your computer and use it in GitHub Desktop.

Select an option

Save mrvdb/77e1f9465bdcbc4aaa859537267ccf9f to your computer and use it in GitHub Desktop.
#!/bin/sh
# This tries to import PGP data contained in a mail message
# Depends on:
# - GPGKEY having the value of my key
# - gpglist (part of signing-party package) (perl scripts)
# -
#
# Programs used
ZN=zenity
NM=notmuch
GPG=gpg
PERL=perl
# Generic error dialog with zenity
function error {
$ZN --error --text="$1"
}
# Try to get signatures from a file
function sigfromfile {
echo file: $1
echo "# Trying direct import"
$GPG --import $1 2>&1
if [[ $? -ne 0 ]]; then
echo "# Plain import failed, trying decrypting it first"
$GPG -d $1 | $GPG --import 2>&1
fi
return $?
}
# Process cmdline args
case $# in
2)
# Both thread and message id
msg_id=$2
# Fallthrough
;&
1)
# Just the thread id
thr_id=$1
;;
*)
error "Got $# arguments. ImportSig takes 1 or 2 arguments (thread_id and optional message_id)"
exit 1
esac
if [[ $msg_id ]]; then
(
echo "Determining message filename"
FILE=`$NM search --exclude=false --output=files "id:$msg_id"`
# Try message file as a whole first
sigfromfile $FILE
if [[ $? -ne 0 ]]; then
echo "# Decrypt message failed, trying attachments explicitly"
T=`mktemp -d`
mu extract --target-dir=$T --save-attachments $FILE
for att in `ls -1 $T`; do
# extract from each attachment
sigfromfile $T/$att
done
fi
echo "# Fetching unknown users"
for KEY in `gpglist $GPGKEY | grep 'User ID not found' | $PERL -nwe '/([0-9A-F]{16})/ && print "$1\n"'`; do
$GPG --recv-keys $KEY 2>&1
gpglist $GPGKEY | grep $KEY
done
echo "# DONE"
) | $ZN --text-info --width 1000 --height 300 --auto-scroll \
--title="Trying to import PGP data from message..." \
--font=Monospace
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment