]> git.donarmstrong.com Git - bin.git/blob - mutt_open
add reset usb bus command
[bin.git] / 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 # from http://git.upsilon.cc/?p=utils/org-mutt.git
11
12 # requires: notmuch | maildir-utils >= 0.7
13
14 MUTT=mutt
15 MAIL_INDEXER="notmuch"  # one of "notmuch", "mu"
16
17 MUTT_FLAGS="-R"
18 HIDE_SIDEBAR_CMD=""     # set to empty string if sidebar is not used
19
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
22
23 die_usage () {
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
29     exit 3
30 }
31
32 # Lookup: Message-ID -> mail path. Store results in global $fname
33 lookup_msgid () {
34     msgid_query="$1"
35     case "$MAIL_INDEXER" in
36         notmuch)
37             fname=$(notmuch search --output=files id:"$msgid_query" | head -n 1)
38             ;;
39         mu)
40             fname=$(mu find -f l i:"$msgid_query" | head -n 1)
41             ;;
42     esac
43 }
44
45 dump_info () {
46     echo "fname: $fname"
47     echo "msgid: $msgid"
48 }
49
50 if [ -z "$1" -o "$1" = "-h" -o "$1" = "-help" -o "$1" = "--help" ] ; then
51     die_usage
52 fi
53 if (echo "$1" | grep -q /) && test -f "$1" ; then       # arg is a file
54     fname="$1"
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
57     msgid="$1"
58     lookup_msgid "$msgid"       # side-effect: set $fname
59 fi
60 # dump_info ; exit 3
61 if ! dirname "$fname" | egrep -q '/(cur|new|tmp)$' ; then
62     echo "Path not pointing inside a maildir: $fname" 1>&2
63     exit 2
64 fi
65 maildir=$(dirname $(dirname "$fname"))
66
67 if ! [ -d "$maildir" ] ; then
68     echo "Not a (mail)dir: $maildir" 1>&1
69     exit 2
70 fi
71
72 msgid=$(echo -n "$msgid"|sed 's/\([\+\*=]\)/\\\\\1/g')
73
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
77 sleep 0.2
78 mutt_keys="$HIDE_SIDEBAR_CMD/=i$msgid\n\n"
79 exec $MUTT $MUTT_FLAGS -f "$maildir/" -e "push $mutt_keys"