3 # Fire up mutt on a given mail, located in some Maildir
4 # Mail can be specified either by path or by Messsage-ID; in the latter case
5 # file lookup is performed using some mail indexing tool.
7 # Copyright: © 2009-2012 Stefano Zacchiroli <zack@upsilon.cc>
8 # License: GNU General Public License (GPL), version 3 or above
10 # requires: notmuch | maildir-utils >= 0.7
13 MAIL_INDEXER="notmuch" # one of "notmuch", "mu"
16 HIDE_SIDEBAR_CMD="B" # set to empty string if sidebar is not used
18 # Sample output of lookup command, which gets passed to mutt-open
19 # /home/zack/Maildir/INBOX/cur/1256673179_0.8700.usha,U=37420,FMD5=7e33429f656f1e6e9d79b29c3f82c57e:2,S
22 echo "Usage: mutt-open FILE" 1>&2
23 echo " mutt-open MESSAGE-ID" 1>&2
24 echo 'E.g.: mutt-open `notmuch search --output=files id:MESSAGE-ID`' 1>&2
25 echo ' mutt-open `mu find -f l i:MESSAGE-ID`' 1>&2
26 echo ' mutt-open 20091030112543.GA4230@usha.takhisis.invalid' 1>&2
30 # Lookup: Message-ID -> mail path. Store results in global $fname
33 case "$MAIL_INDEXER" in
35 fname=$(notmuch search --output=files id:"$msgid_query" | head -n 1)
38 fname=$(mu find -f l i:"$msgid_query" | head -n 1)
48 if [ -z "$1" -o "$1" = "-h" -o "$1" = "-help" -o "$1" = "--help" ] ; then
51 if (echo "$1" | grep -q /) && test -f "$1" ; then # arg is a file
53 msgid=$(egrep -i '^message-id:' "$fname" | cut -f 2 -d':' | sed 's/[ <>]//g')
54 elif ! (echo "$1" | grep -q /) ; then # arg is a Message-ID
56 lookup_msgid "$msgid" # side-effect: set $fname
59 if ! dirname "$fname" | egrep -q '/(cur|new|tmp)$' ; then
60 echo "Path not pointing inside a maildir: $fname" 1>&2
63 maildir=$(dirname $(dirname "$fname"))
65 if ! [ -d "$maildir" ] ; then
66 echo "Not a (mail)dir: $maildir" 1>&1
70 # UGLY HACK: without sleep, push keys do not reach mutt, I _guess_ that there
71 # might be some terminal-related issue here, since also waiting for an input
72 # with "read" similarly "solves" the problem
74 mutt_keys="$HIDE_SIDEBAR_CMD/~i$msgid\n\n"
75 exec $MUTT $MUTT_FLAGS -f "$maildir/" -e "push $mutt_keys"