Skip to content

Instantly share code, notes, and snippets.

@lucc
Created December 30, 2016 09:21
Show Gist options
  • Save lucc/89437c04af71ec4e818b1c0c31c01f69 to your computer and use it in GitHub Desktop.
Save lucc/89437c04af71ec4e818b1c0c31c01f69 to your computer and use it in GitHub Desktop.
Small wrapper for fast address lookup in mutt/alot/... of the addresses in khard and gpg keyring,
#!/usr/bin/make -f
# A rewimplementation of the idea of lbdb in as a make file.
CACHE = ~/.cache/lbdb
SHARE = \
/usr/share \
/usr/local/share \
~/.local/share
# This names all the methods used and also their priority. The first an email
# address is found in several sources only the first one will be used.
LISTS = \
khard \
gpg \
inmail \
DONTBUILD = \
$(GNUPGHOME)/pubring.kbx \
~/.local/share/khard/main/*.vcf \
$(MAILS)
SORT = sort --uniq --field-separator=' ' --key=1,1
$(CACHE)/lbdb: $(patsubst %,$(CACHE)/%.sorted,$(LISTS))
$(SORT) $^ > $@
$(CACHE)/inmail.list: ~/.config/lbdb/m_inmail.list
cp -f $< $@
$(CACHE)/khard.list: ~/.local/share/khard/main/????????????????????????????????????.vcf
khard email --parsable --display first_name --remove-first-line \
| sed 's/\t[^\t]*$$/\t@/' \
> $@
ifndef GNUPGHOME
GNUPGHOME = ~/.gnupg
endif
$(CACHE)/gpg.list: $(GNUPGHOME)/pubring.kbx
gpg --list-keys --with-colons 2>/dev/null \
| sed -n -e '/^\(pub\|uid\):[^re]:\([^:]*:\)\{7,7\}[^<>:]* <[^<>@: ]*@[^<>@: ]*>[^<>@:]*:/ {' \
-e ' s/^\([^:]*:\)\{9,9\}\([^<:]*\) <\([^>:]*\)>.*:.*$$/\3 \2 (GnuPG)/' \
-e ' s/ \([^ ]\{27,27\}\)[^ ]* / \1... /' \
-e ' p' \
-e '}' \
> $@
# The *.list files depend on the cache directory.
$(foreach target,$(LISTS),$(eval $(CACHE)/$(target).list: $(CACHE)/.dir))
# Some entries need to be normalized.
$(CACHE)%.normalized: $(CACHE)/%.list
cat $< > $@
# Sort each source individually, just in case we might need it.
$(CACHE)/%.sorted: $(CACHE)/%.normalized
$(SORT) < $< > $@
%/.dir:
mkdir -p $*
touch $@
$(DONTBUILD):;
clear-cache:
$(RM) -fr $(CACHE)
cache-statistics: $(CACHE)/lbdb
wc $(patsubst %,$(CACHE)/%.sorted,$(LISTS))
wc $(CACHE)/lbdb
#!/bin/bash
# Wrapper (command line interface) for the lbdb.mk file.
prog=${0##*/}
makefile=${0%.sh}.mk
version=0.2
grep_options=( -i -a )
usage () {
echo "$prog [-x] search terms"
echo "$prog -h"
echo "$prog -v"
}
help () {
echo "Search the little brothers data base for a matching email address."
echo "Options: -x for debugging, -h for help, -v for version."
echo "Search terms are used by grep(1) in case insensitive mode."
}
grep_chain () {
if [[ $# -eq 1 ]]; then
grep "${grep_options[@]}" "$1"
else
local pattern=$1
shift
grep "${grep_options[@]}" "$pattern" | grep_chain "$@"
fi
}
while getopts hvx FLAG; do
case $FLAG in
h) usage; echo; help; exit;;
v) echo "$prog $version"; echo "Using $(grep --version|head -n 1)"; exit;;
x) set -x;;
*) usage >&2; exit 2;;
esac
done
shift $((OPTIND - 1))
if [ $# -lt 1 ]; then
usage >&2
exit 2
fi
make --file "$makefile" --quiet
grep_chain "$@" < ~/.cache/lbdb/lbdb
@lucc
Copy link
Author

lucc commented Feb 6, 2018

I have since created a full blown repository: https://github.com/lucc/abq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment