dotfiles/mail/.local/bin/mail-searchcontacts
Marty Oehme be688572f5
mail: Add contacts lookup script
Added `mail-searchcontacts` script, which uses notmuch to search for all
contacts of the mail database.

Can be invoked without any arguments to list *all* contacts of the
dataase in descending order of message count.
Otherwise, it will filter known contacts, first by prioritizing the
'from:' field, and otherwise by searching through the whole database for
the keywords passed in.

It will also re-format the results so that the list consists of
tab-delineated 'mail name' rows. If there is no name, the row will only
contain the bare e-mail address. This format is best accepted by aerc,
for which the script is written.

aerc uses the contacts script for its address completion in editing
e-mail headers.

Renamed `checkmail` to `mail-check` to fit the naming scheme of
`mail-[action]` for mail scripts, to better be able to group them in the
shell.
2020-09-23 17:55:06 +02:00

34 lines
1 KiB
Bash
Executable file

#!/usr/bin/env sh
# gets known e-mail adresses out of the current notmuch database
#
# called like `mail-searchcontacts [myadress]`
#
# where myaddress can be anything that connects mails to their adresses:
# if directly found in the 'from': field, it will return those adresses
# if no results are returned, it will switch to a more fuzzy search mode
# looking through subjects, mail text, and so on for the keywords
#
# this can be somewhat slow for large mailboxes, in which case you probably
# want to turn off fuzzy matching
searchterm="$1"
# make search fuzzy if 'fuzzy' is the second arg
searchcmd() {
if [ -n "$1" ] && [ "$2" = "fuzzy" ]; then
term="and $1"
elif [ -n "$1" ]; then
term="and from:$1"
else
term=""
fi
notmuch address --deduplicate=address --output=count not from:marty.oehme@gmail.com "$term" | sort --numeric-sort -r | cut -f2- | sed -e 's/\(^.*\)\(<.*$\)/\2\t\1/'
}
results=$(searchcmd "$searchterm")
if [ -z "$results" ]; then
results=$(searchcmd "$searchterm" "fuzzy")
fi
echo "$results"