2 # written by Johannes Weißl
6 # This script makes mail indexers (like mairix, mu or nmzmail) together with
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).
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:
19 # macro generic ,j "<enter-command>push <pipe-message>muttjump<enter><enter>" "jump to original message"
21 # one of: mairix, mu, mu-old (mu < 0.7) and nmzmail
22 MUTTJUMP_INDEXER=${MUTTJUMP_INDEXER:-}
24 # "limit" or "search" (default)
25 MUTTJUMP_MODE=${MUTTJUMP_MODE:-search}
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}
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}
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 () {
45 # Version of GNU screen
46 # Since 4.01.00devel (commit 98b6b41) backslash for -X handling was altered.
47 MUTTJUMP_SCREEN_VERSION=${MUTTJUMP_SCREEN_VERSION:-auto}
51 MAIRIX=${MAIRIX:-mairix}
53 NMZMAIL=${NMZMAIL:-nmzmail}
54 SCREEN=${SCREEN:-screen}
55 FORMAIL=${FORMAIL:-formail}
56 REFORMAIL=${REFORMAIL:-reformail}
57 WHIPTAIL=${WHIPTAIL:-whiptail --noitem}
58 DIALOG=${DIALOG:-dialog}
66 echo \'${1//\'/\'\\\'\'}\'
69 function quote_regexp () {
70 echo "$1" | sed 's/[][\^.$|)(*+?}{]/\\&/g'
73 function is_callable () {
74 type $1 >/dev/null 2>&1
78 awk '{ y = length(); if (y > x) x = y } END { print x }'
81 # Check if screen is new enough to parse -X (commit 98b6b41) and support
82 # -Q (commit 8147d08). It (falsely) assumes that screen version > 4.00
83 # does this. Distributions that use git versions are likely to also keep
85 function is_new_screen () {
86 sv=$MUTTJUMP_SCREEN_VERSION
87 if [ "$sv" = auto ] ; then
88 sv=$($SCREEN --version | awk '{print $3}')
90 echo "$sv" | grep -vq "^\(4\.00\|[0-3]\.\)"
96 cat msg | muttjump [-R]
98 This script calls mutt, jumping to the message given in stdin or to the message
99 identified by "msgid". With "-R", it jumps to the message being replied to.
100 It uses a mail search engine (currently mairix, mu and nmzmail are supported),
101 which has to be configured through MUTTJUMP_INDEXER variable.
106 function reopen_tty () {
107 # Close message-stdin and open terminal-stdin instead.
108 # mutt behaves different if STDIN is no terminal
109 # TODO: Find cleaner solution (e.g. mutt command-line argument?)
111 term="/dev/$(ps -p$$ -otty=)"
116 # Check command-line arguments and STDIN
117 search_header="Message-ID"
119 [ $# -ne 1 ] && usage
121 [ $# -gt 1 ] && usage
122 if [ $# -eq 1 ] ; then
123 [ "$1" != "-R" ] && usage
124 search_header="In-Reply-To"
129 # check if mutt is installed
130 if ! is_callable $MUTT ; then
131 die "$MUTT is not in PATH, set MUTT variable"
135 if is_callable $WHIPTAIL ; then
136 DIALOG_PROG=$WHIPTAIL
137 elif is_callable $DIALOG ; then
141 case $MUTTJUMP_MODE in
145 die "variable MUTTJUMP_MODE must be set to \"limit\" or \"search\""
149 # search for Message-ID in STDIN
150 if [ $# -eq 1 ] ; then
152 elif is_callable $FORMAIL ; then
153 msgid=$($FORMAIL -c -z -x $search_header | head -n1)
154 elif is_callable $REFORMAIL ; then
155 msgid=$($REFORMAIL -c -x $search_header: | head -n1)
157 msgid=$(sed -n 's/^'$search_header':[ \t]*\(.*\)/\1/Ip' | head -n1)
159 if [ -z "$msgid" ] ; then
160 die "could not find $search_header header in standard input"
162 msgid_quoted=$(quote_regexp "$msgid")
163 msgid_clean=$(echo "$msgid" | sed -e 's/^<//' -e 's/>$//')
165 # try to locate path of message using a mail search engine
166 case $MUTTJUMP_INDEXER in
168 msgid_mairix=$msgid_clean
169 # mairix can't quote special characters (~,/=^) in search words. As a
170 # workaround, split Message-ID in 31-character long AND-linked
172 if echo "$msgid_clean" | grep -q '[~,/=^]' ; then
173 msgid_mairix=$(echo "$msgid_clean" | sed -e 's/[~,/=^]\+/ /g' \
174 -e 's/[^ ]\{31\}/& /g' -e 's/^ \+//g' -e 's/ *$/=/g' \
177 orig_msgfiles=$($MAIRIX -r "m:$msgid_mairix")
180 orig_msgfiles=$($MU find -f l "i:$msgid_clean" | grep -v "^\*\*")
183 orig_msgfiles=$($MU find -f p "m:$msgid_clean")
186 nmzmail_results=$(mktemp -d)
187 echo "+message-id:/^$msgid_quoted$/" | $NMZMAIL -r "$nmzmail_results"
188 orig_msgfiles=$(find "$nmzmail_results" -type l -exec readlink {} \;)
189 rm -rf "$nmzmail_results"
192 die "variable MUTTJUMP_INDEXER not set or empty"
195 die "unknown mail index program \"$MUTTJUMP_INDEXER\""
199 if [ -z "$orig_msgfiles" ] ; then
200 die "no message with msgid $msgid found!"
203 count=$(echo "$orig_msgfiles" | wc -l)
204 if [ $count -gt 1 -a -n "$DIALOG_PROG" ] ; then
205 choices=$(echo "$orig_msgfiles" | while read line ;
206 do dirname "$(dirname "$line")" ; echo . ; done)
207 maxwidth=$(echo "$choices" | wc_L)
208 orig_maildir=$($DIALOG_PROG --clear --title "More than one mailbox found" \
209 --menu "Select mailbox:" $((count+8)) $((maxwidth+16)) \
210 $count $choices 3>&2 2>&1 1>&3-)
211 elif [ $count -gt 1 ] ; then
214 choices=("${choices[@]}" "$(dirname "$(dirname "$line")")")
215 done <<<"$orig_msgfiles"
216 echo "More than one mailbox found"
217 echo "Select mailbox:"
218 select md in "${choices[@]}" ; do
223 orig_msgfile=$orig_msgfiles
224 # get containing maildir of $orig_msgfile
225 orig_maildir=$(dirname "$(dirname "$orig_msgfile")")
228 if [ ! -d "$orig_maildir/cur" ] ; then
229 die "directory $(quote "$orig_maildir") doesn't exist or is no Maildir"
232 msgid_mutt=$(quote_regexp "$msgid_quoted")
233 jump_expr="~i'$msgid_mutt'"
234 jump_cmd="<limit>$jump_expr<enter>"
235 if [ "$MUTTJUMP_MODE" = search ] ; then
236 jump_cmd="$jump_cmd<limit>all<enter>"
239 if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = yes ] ||
240 [ "$MUTTJUMP_USE_SCREEN" = auto -a -n "$STY" ] ; then
241 MUTTJUMP_USE_SCREEN=yes
246 if [ "$MUTTJUMP_USE_SCREEN" = yes ] ; then
247 if [ -n "$STY" ] ; then
248 screen_opts=("-X" "screen")
249 if is_new_screen ; then
250 jump_cmd=${jump_cmd/\\/\\\\\\}
251 screen_query_arg="-Q"
256 screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
257 screen_opts=("${screen_opts[@]}" "-t" "$screen_window_name")
263 if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = yes -a -n "$STY" ] ; then
264 $SCREEN -X -p "$screen_window_name" $screen_query_arg \
265 select "$screen_window_name" >/dev/null
267 $SCREEN -X -p "$screen_window_name" stuff ":push \"$jump_cmd\"
\r"
272 # start mutt, open original folder and jump to the original message
273 $SCREEN "${screen_opts[@]}" $MUTT -f "$orig_maildir" -e "push \"$jump_cmd\""