Added support for using notmuch as an external indexer.
[muttjump] / muttjump
1 #!/bin/bash
2 # written by Johannes Weißl
3
4 # muttjump
5 #
6 # This script makes mail indexers (like mairix, mu, nmzmail, or
7 # notmuch) together with mutt more useful.
8 #
9 # These search engines usually create a virtual maildir containing symbolic
10 # links to the original mails, which can be browsed using mutt.
11 # It would be optimal if mutt somehow knew that the two maildir entries
12 # identify the same mail, but this is not that easy (mail folder
13 # abstraction from different formats, no tight integration of mail indexers).
14 #
15 # So if one wants to rename (for setting/clearing flags), delete or edit the
16 # mails, it is only possible using the original mail. This (very simple)
17 # script helps to jump to this message, using e.g. this macro in .muttrc:
18 #
19 # macro generic ,j "<enter-command>push <pipe-message>muttjump<enter><enter>" "jump to original message"
20
21 # one of: mairix, mairix-git, mu, mu-old (mu < 0.7), nmzmail or notmuch (>0.5)
22 MUTTJUMP_INDEXER=${MUTTJUMP_INDEXER:-}
23
24 # "limit" or "search" (default)
25 MUTTJUMP_MODE=${MUTTJUMP_MODE:-search}
26
27 # "yes", "no" or "auto"
28 # If set to "auto", the $STY environment variable is used for auto detection.
29 MUTTJUMP_USE_SCREEN=${MUTTJUMP_USE_SCREEN:-auto}
30
31 # "yes" or "no"
32 # Set this to "yes" if you want exactly one screen window (with mutt running
33 # inside) per mailbox. With new versions of screen (> 4.00) it is possible
34 # to do that automatically, with old versions you have to start them
35 # manually! Setting this to yes implies MUTTJUMP_USE_SCREEN=yes.
36 MUTTJUMP_MULTI_SCREEN_MODE=${MUTTJUMP_MULTI_SCREEN_MODE:-no}
37
38 # function to create the screen window name from the full path of the mailbox
39 if ! type MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE >/dev/null 2>&1 ; then
40     function MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE () {
41         basename "$1"
42     }
43 fi
44
45 # Version of GNU screen
46 # Since 4.01.00devel (commit 98b6b41) backslash handling for -X was altered.
47 MUTTJUMP_SCREEN_VERSION=${MUTTJUMP_SCREEN_VERSION:-auto}
48
49 # program paths
50 MUTT=${MUTT:-mutt}
51 MAIRIX=${MAIRIX:-mairix}
52 MU=${MU:-mu}
53 NMZMAIL=${NMZMAIL:-nmzmail}
54 NOTMUCH=${NOTMUCH:-notmuch}
55 SCREEN=${SCREEN:-screen}
56 FORMAIL=${FORMAIL:-formail}
57 REFORMAIL=${REFORMAIL:-reformail}
58 WHIPTAIL=${WHIPTAIL:-whiptail --noitem}
59 DIALOG=${DIALOG:-dialog}
60
61 function die () {
62     echo -e >&2 "$0: $1"
63     exit 1
64 }
65
66 function quote () {
67     echo \'${1//\'/\'\\\'\'}\'
68 }
69
70 function quote_regexp () {
71     echo "$1" | sed 's/[][\^.$|)(*+?}{]/\\&/g'
72 }
73
74 function is_callable () {
75     type $1 >/dev/null 2>&1
76 }
77
78 function wc_L () {
79     awk '{ y = length(); if (y > x) x = y } END { print x }'
80 }
81
82 # Check if screen is new enough to parse -X (commit 98b6b41) and support
83 # -Q (commit 8147d08). It (falsely) assumes that screen version > 4.00
84 # does this. Distributions that use git versions are likely to also keep
85 # them up to date.
86 function is_new_screen () {
87     sv=$MUTTJUMP_SCREEN_VERSION
88     if [ "$sv" = auto ] ; then
89         sv=$($SCREEN --version | awk '{print $3}')
90     fi
91     echo "$sv" | grep -vq "^\(4\.00\|[0-3]\.\)"
92 }
93
94 function usage () {
95     cat >&2 <<END
96 Usage: muttjump msgid
97        cat msg | muttjump [-R]
98
99 This script calls mutt, jumping to the message given in stdin or to the message
100 identified by "msgid".  With "-R", it jumps to the message being replied to.
101 It uses a mail search engine (currently mairix, mu and nmzmail are supported),
102 which has to be configured through MUTTJUMP_INDEXER variable.
103 END
104     exit 1
105 }
106
107 function reopen_tty () {
108     # Close message-stdin and open terminal-stdin instead.
109     # mutt behaves different if STDIN is no terminal
110     # TODO: Find cleaner solution (e.g. mutt command-line argument?)
111     exec 0<&-
112     term="/dev/$(ps -p$$ -otty=)"
113     exec < $term
114 }
115
116
117 # Check command-line arguments and STDIN
118 search_header="Message-ID"
119 if tty -s ; then
120     [ $# -ne 1 ] && usage
121 else
122     [ $# -gt 1 ] && usage
123     if [ $# -eq 1 ] ; then
124         [ "$1" != "-R" ] && usage
125         search_header="In-Reply-To"
126         shift
127     fi
128 fi
129
130 # check if mutt is installed
131 if ! is_callable $MUTT ; then
132     die "$MUTT is not in PATH, set MUTT variable"
133 fi
134
135 DIALOG_PROG=""
136 if is_callable $WHIPTAIL ; then
137     DIALOG_PROG=$WHIPTAIL
138 elif is_callable $DIALOG ; then
139     DIALOG_PROG=$DIALOG
140 fi
141
142 case $MUTTJUMP_MODE in
143     limit|search)
144         ;;
145     *)
146         die "variable MUTTJUMP_MODE must be set to \"limit\" or \"search\""
147         ;;
148 esac
149
150 # search for Message-ID in STDIN
151 if [ $# -eq 1 ] ; then
152     msgid=$1
153 elif is_callable $FORMAIL ; then
154     msgid=$($FORMAIL -c -z -x $search_header | head -n1)
155 elif is_callable $REFORMAIL ; then
156     msgid=$($REFORMAIL -c -x $search_header: | head -n1)
157 else
158     msgid=$(sed -n 's/^'$search_header':[ \t]*\(.*\)/\1/Ip' | head -n1)
159 fi
160 if [ -z "$msgid" ] ; then
161     die "could not find $search_header header in standard input"
162 fi
163 msgid_quoted=$(quote_regexp "$msgid")
164 msgid_clean=$(echo "$msgid" | sed -e 's/^<//' -e 's/>$//')
165
166 # try to locate path of message using a mail search engine
167 case $MUTTJUMP_INDEXER in
168     mairix-git)
169         orig_msgfiles=$($MAIRIX -r "m:$msgid_clean")
170         ;;
171     mairix)
172         msgid_mairix=$msgid_clean
173         # mairix can't quote special characters (~,/=^) in search words. As a
174         # workaround, split Message-ID in 31-character long AND-linked
175         # substrings.
176         if echo "$msgid_clean" | grep -q '[~,/=^]' ; then
177             msgid_mairix=$(echo "$msgid_clean" | sed -e 's/[~,/=^]\+/ /g' \
178                 -e 's/[^ ]\{31\}/& /g' -e 's/^ \+//g' -e 's/ *$/=/g' \
179                 -e 's/ /=,/g')
180         fi
181         orig_msgfiles=$($MAIRIX -r "m:$msgid_mairix")
182         ;;
183     mu)
184         orig_msgfiles=$($MU find -f l "i:$msgid_clean" | grep -v "^\*\*")
185         ;;
186     mu-old)
187         orig_msgfiles=$($MU find -f p "m:$msgid_clean")
188         ;;
189     nmzmail)
190         nmzmail_results=$(mktemp -d)
191         echo "+message-id:/^$msgid_quoted$/" | $NMZMAIL -r "$nmzmail_results"
192         orig_msgfiles=$(find "$nmzmail_results" -type l -exec readlink {} \;)
193         rm -rf "$nmzmail_results"
194         ;;
195     notmuch)
196         orig_msgfiles=$($NOTMUCH search --output=files "id:$msgid_clean")
197         ;;
198     "")
199         die "variable MUTTJUMP_INDEXER not set or empty"
200         ;;
201     *)
202         die "unknown mail index program \"$MUTTJUMP_INDEXER\""
203         ;;
204 esac
205
206 if [ -z "$orig_msgfiles" ] ; then
207     die "no message with msgid $msgid found!"
208 fi
209
210 count=$(echo "$orig_msgfiles" | wc -l)
211 if [ $count -gt 1 -a -n "$DIALOG_PROG" ] ; then
212     choices=$(echo "$orig_msgfiles" | while read line ;
213         do dirname "$(dirname "$line")" ; echo . ; done)
214     maxwidth=$(echo "$choices" | wc_L)
215     orig_maildir=$($DIALOG_PROG --clear --title "More than one mailbox found" \
216         --menu "Select mailbox:" $((count+8)) $((maxwidth+16)) \
217         $count $choices 3>&2 2>&1 1>&3-)
218 elif [ $count -gt 1 ] ; then
219     choices=()
220     while read line ; do
221         choices=("${choices[@]}" "$(dirname "$(dirname "$line")")")
222     done <<<"$orig_msgfiles"
223     echo "More than one mailbox found"
224     echo "Select mailbox:"
225     select md in "${choices[@]}" ; do
226         orig_maildir=$md
227         break
228     done <> /dev/tty
229 else
230     orig_msgfile=$orig_msgfiles
231     # get containing maildir of $orig_msgfile
232     orig_maildir=$(dirname "$(dirname "$orig_msgfile")")
233 fi
234
235 if [ ! -d "$orig_maildir/cur" ] ; then
236     die "directory $(quote "$orig_maildir") doesn't exist or is no Maildir"
237 fi
238
239 msgid_mutt=$(quote_regexp "$msgid_quoted")
240 jump_expr="~i'$msgid_mutt'"
241 jump_cmd="<limit>$jump_expr<enter>"
242 if [ "$MUTTJUMP_MODE" = search ] ; then
243     jump_cmd="$jump_cmd<limit>all<enter>"
244 fi
245
246 if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = yes ] ||
247         [ "$MUTTJUMP_USE_SCREEN" = auto -a -n "$STY" ] ; then
248     MUTTJUMP_USE_SCREEN=yes
249 fi
250
251 screen_opts=()
252 screen_query_arg=""
253 if [ "$MUTTJUMP_USE_SCREEN" = yes ] ; then
254     if [ -n "$STY" ] ; then
255         screen_opts=("-X" "screen")
256         if is_new_screen ; then
257             jump_cmd=${jump_cmd/\\/\\\\\\}
258             screen_query_arg="-Q"
259         fi
260     else
261         reopen_tty
262     fi
263     screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
264     screen_opts=("${screen_opts[@]}" "-t" "$screen_window_name")
265 else
266     SCREEN=""
267     reopen_tty
268 fi
269
270 if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = yes -a -n "$STY" ] ; then
271     $SCREEN -X -p "$screen_window_name" $screen_query_arg \
272             select "$screen_window_name" >/dev/null
273     if [ $? = 0 ] ; then
274         $SCREEN -X -p "$screen_window_name" stuff ":push \"$jump_cmd\"\r"
275         exit 0
276     fi
277 fi
278
279 # start mutt, open original folder and jump to the original message
280 $SCREEN "${screen_opts[@]}" $MUTT -f "$orig_maildir" -e "push \"$jump_cmd\""

Benjamin Mako Hill || Want to submit a patch?