From be688572f5cd5aeb5264f81946ae98b6fbaceebd Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 18 Sep 2020 09:48:25 +0200 Subject: [PATCH] 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. --- mail/.config/aerc/aerc.conf | 2 +- mail/.config/aerc/binds.conf | 2 +- mail/.local/bin/{checkmail => mail-check} | 9 +++++- mail/.local/bin/mail-searchcontacts | 34 +++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) rename mail/.local/bin/{checkmail => mail-check} (79%) create mode 100755 mail/.local/bin/mail-searchcontacts diff --git a/mail/.config/aerc/aerc.conf b/mail/.config/aerc/aerc.conf index d83831c..d7effe8 100644 --- a/mail/.config/aerc/aerc.conf +++ b/mail/.config/aerc/aerc.conf @@ -140,7 +140,7 @@ header-layout=To|From,Subject,Cc|Bcc # the first field. Only the email address field is required. The second field, # if present, will be treated as the contact name. Additional fields are # ignored. -address-book-cmd= +address-book-cmd=mail-searchcontacts %s [filters] # diff --git a/mail/.config/aerc/binds.conf b/mail/.config/aerc/binds.conf index 122aba4..e39cf31 100644 --- a/mail/.config/aerc/binds.conf +++ b/mail/.config/aerc/binds.conf @@ -4,7 +4,7 @@ gT = :prev-tab gt = :next-tab = :term -gR = :exec checkmail +gR = :exec mail-check [messages] q = :quit diff --git a/mail/.local/bin/checkmail b/mail/.local/bin/mail-check similarity index 79% rename from mail/.local/bin/checkmail rename to mail/.local/bin/mail-check index 27ae526..fc90d16 100755 --- a/mail/.local/bin/checkmail +++ b/mail/.local/bin/mail-check @@ -13,7 +13,7 @@ MBSYNC_MAX_TRIES=3 MBSYNC_PRE="imapfilter -c /home/marty/.config/imapfilter/config.lua" -MBSYNC_POST="notmuch new" +MBSYNC_POST="notmuch new && afew --tag --new" # fail the routine and optionally send a message why fail() { @@ -59,4 +59,11 @@ main() { $MBSYNC_POST } +notmuch_foldertags() { + notmuch tag +dump -inbox -deleted -- folder:Dump and not folder:Inbox + notmuch tag +archived -inbox -deleted -- folder:Archive and not folder:Inbox + notmuch tag +deleted -inbox -archived -- folder:Trash and not folder:Inbox +} + main +notmuch_foldertags diff --git a/mail/.local/bin/mail-searchcontacts b/mail/.local/bin/mail-searchcontacts new file mode 100755 index 0000000..65c1f3c --- /dev/null +++ b/mail/.local/bin/mail-searchcontacts @@ -0,0 +1,34 @@ +#!/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"