Updated scripts to use capture instead of remember
[org-mutt] / mutt-open
1 #!/bin/bash
2 #
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.
6 #
7 # Copyright: © 2009-2012 Stefano Zacchiroli <zack@upsilon.cc>
8 # License: GNU General Public License (GPL), version 3 or above
9
10 # requires: notmuch | maildir-utils >= 0.7
11
12 MUTT=mutt
13 MAIL_INDEXER="notmuch"  # one of "notmuch", "mu"
14
15 MUTT_FLAGS="-R"
16 HIDE_SIDEBAR_CMD="B"    # set to empty string if sidebar is not used
17
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
20
21 die_usage () {
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
27     exit 3
28 }
29
30 # Lookup: Message-ID -> mail path. Store results in global $fname
31 lookup_msgid () {
32     msgid_query="$1"
33     case "$MAIL_INDEXER" in
34         notmuch)
35             fname=$(notmuch search --output=files id:"$msgid_query" | head -n 1)
36             ;;
37         mu)
38             fname=$(mu find -f l i:"$msgid_query" | head -n 1)
39             ;;
40     esac
41 }
42
43 dump_info () {
44     echo "fname: $fname"
45     echo "msgid: $msgid"
46 }
47
48 if [ -z "$1" -o "$1" = "-h" -o "$1" = "-help" -o "$1" = "--help" ] ; then
49     die_usage
50 fi
51 if (echo "$1" | grep -q /) && test -f "$1" ; then       # arg is a file
52     fname="$1"
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
55     msgid="$1"
56     lookup_msgid "$msgid"       # side-effect: set $fname
57 fi
58 # dump_info ; exit 3
59 if ! dirname "$fname" | egrep -q '/(cur|new|tmp)$' ; then
60     echo "Path not pointing inside a maildir: $fname" 1>&2
61     exit 2
62 fi
63 maildir=$(dirname $(dirname "$fname"))
64
65 if ! [ -d "$maildir" ] ; then
66     echo "Not a (mail)dir: $maildir" 1>&1
67     exit 2
68 fi
69
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
73 sleep 0.1
74 mutt_keys="$HIDE_SIDEBAR_CMD/~i$msgid\n\n"
75 exec $MUTT $MUTT_FLAGS -f "$maildir/" -e "push $mutt_keys"

Benjamin Mako Hill || Want to submit a patch?