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 # from http://git.upsilon.cc/?p=utils/org-mutt.git
12 # requires: notmuch | maildir-utils >= 0.7
15 MAIL_INDEXER="notmuch" # one of "notmuch", "mu"
18 HIDE_SIDEBAR_CMD="" # set to empty string if sidebar is not used
20 # Sample output of lookup command, which gets passed to mutt-open
21 # /home/zack/Maildir/INBOX/cur/1256673179_0.8700.usha,U=37420,FMD5=7e33429f656f1e6e9d79b29c3f82c57e:2,S
24 echo "Usage: mutt-open FILE" 1>&2
25 echo " mutt-open MESSAGE-ID" 1>&2
26 echo 'E.g.: mutt-open `notmuch search --output=files id:MESSAGE-ID`' 1>&2
27 echo ' mutt-open `mu find -f l i:MESSAGE-ID`' 1>&2
28 echo ' mutt-open 20091030112543.GA4230@usha.takhisis.invalid' 1>&2
32 # Lookup: Message-ID -> mail path. Store results in global $fname
35 case "$MAIL_INDEXER" in
37 fname=$(notmuch search --output=files id:"$msgid_query" | head -n 1)
40 fname=$(mu find -f l i:"$msgid_query" | head -n 1)
50 if [ -z "$1" -o "$1" = "-h" -o "$1" = "-help" -o "$1" = "--help" ] ; then
53 if (echo "$1" | grep -q /) && test -f "$1" ; then # arg is a file
55 msgid=$(egrep -i '^message-id:' "$fname" | cut -f 2 -d':' | sed 's/[ <>]//g')
56 elif ! (echo "$1" | grep -q /) ; then # arg is a Message-ID
58 lookup_msgid "$msgid" # side-effect: set $fname
61 if ! dirname "$fname" | egrep -q '/(cur|new|tmp)$' ; then
62 echo "Path not pointing inside a maildir: $fname" 1>&2
65 maildir=$(dirname $(dirname "$fname"))
67 if ! [ -d "$maildir" ] ; then
68 echo "Not a (mail)dir: $maildir" 1>&1
72 msgid=$(echo -n "$msgid"|sed 's/\([\+\*=]\)/\\\\\1/g')
74 # UGLY HACK: without sleep, push keys do not reach mutt, I _guess_ that there
75 # might be some terminal-related issue here, since also waiting for an input
76 # with "read" similarly "solves" the problem
78 mutt_keys="$HIDE_SIDEBAR_CMD/=i$msgid\n\n"
79 exec $MUTT $MUTT_FLAGS -f "$maildir/" -e "push $mutt_keys"