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 # If this is set to "yes", muttjump will not open a new instance of
33 # mutt, but instead jump to the original message in a running mutt
34 # (which is running in a screen session).
35 # ATTENTION: You need to have one mutt instance open for every mailbox
37 MUTTJUMP_MULTI_SCREEN_MODE=${MUTTJUMP_MULTI_SCREEN_MODE:-no}
39 # name of the screen session (screen -S ...), leave blank for none
40 MUTTJUMP_SCREEN_SESSION=${MUTTJUMP_SCREEN_SESSION:-}
42 # function to create the screen window name from the full path of the mailbox
43 if ! type MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE >/dev/null 2>&1 ; then
44 function MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE () {
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 is_callable () {
70 type $1 >/dev/null 2>&1
76 cat msg | muttjump [-R]
78 This script calls mutt, jumping to the message given in stdin or to the message
79 identified by "msgid". With "-R", it jumps to the message being replied to.
80 It uses a mail search engine (currently mairix, mu and nmzmail are supported),
81 which has to be configured through MUTTJUMP_INDEXER variable.
87 # Check command-line arguments and STDIN
88 search_header="Message-ID"
93 if [ $# -eq 1 ] ; then
94 [ "$1" != "-R" ] && usage
95 search_header="In-Reply-To"
100 # check if mutt is installed
101 if ! is_callable $MUTT ; then
102 die "$MUTT is not in PATH, set MUTT variable"
106 if is_callable $WHIPTAIL ; then
107 DIALOG_PROG=$WHIPTAIL
108 elif is_callable $DIALOG ; then
112 case $MUTTJUMP_MODE in
116 die "variable MUTTJUMP_MODE must be set to \"limit\" or \"search\""
120 # search for Message-ID in STDIN
121 if [ $# -eq 1 ] ; then
123 elif is_callable $FORMAIL ; then
124 msgid=$($FORMAIL -c -z -x $search_header | head -n1)
125 elif is_callable $REFORMAIL ; then
126 msgid=$($REFORMAIL -c -x $search_header: | head -n1)
128 msgid=$(sed -n 's/^'$search_header':[ \t]*\(.*\)/\1/Ip' | head -n1)
130 if [ -z "$msgid" ] ; then
131 die "could not find $search_header header in standard input"
133 msgid_clean=$(echo "$msgid" | sed -e 's/^<//' -e 's/>$//')
135 # try to locate path of message using a mail search engine
136 case $MUTTJUMP_INDEXER in
138 orig_msgfiles=$($MAIRIX -r "m:$msgid_clean")
141 orig_msgfiles=$($MU find -f l "i:$msgid_clean" | grep -v "^\*\*")
144 orig_msgfiles=$($MU find -f p "m:$msgid_clean")
147 nmzmail_results=$(mktemp -d)
148 echo "+message-id:$msgid" | $NMZMAIL -r "$nmzmail_results"
149 orig_msgfiles=$(find "$nmzmail_results" -type l -exec readlink {} \;)
150 rm -rf "$nmzmail_results"
153 die "variable MUTTJUMP_INDEXER not set or empty"
156 die "unknown mail index program \"$MUTTJUMP_INDEXER\""
160 if [ -z "$orig_msgfiles" ] ; then
161 die "no message with msgid $msgid found!"
164 count=$(echo "$orig_msgfiles" | wc -l)
165 if [ $count -gt 1 -a -n "$DIALOG_PROG" ] ; then
166 choices=$(echo "$orig_msgfiles" | while read line ;
167 do echo -e "$(dirname "$(dirname "$line")")\n." ; done)
168 maxwidth=$(echo "$choices" | wc -L)
169 orig_maildir=$($DIALOG_PROG --clear --title "More than one mailbox found" \
170 --menu "Select mailbox:" $((count+8)) $((maxwidth+16)) \
171 $count $choices 3>&2 2>&1 1>&3-)
173 orig_msgfile=$(echo "$orig_msgfiles" | head -n1)
174 # get containing maildir of $orig_msgfile
175 orig_maildir=$(dirname "$(dirname "$orig_msgfile")")
178 if [ ! -d "$orig_maildir/cur" ] ; then
179 die "directory $(quote "$orig_maildir") doesn't exist or is no Maildir"
182 jump_expr="~i'$msgid'"
184 if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = no ] ; then
186 jump_cmd="<limit>$jump_expr<enter>"
187 if [ "$MUTTJUMP_MODE" = search ] ; then
188 jump_cmd="$jump_cmd<limit>all<enter>"
191 if [ "$MUTTJUMP_USE_SCREEN" = auto -a -n "$STY" ] ; then
192 MUTTJUMP_USE_SCREEN=yes
196 if [ "$MUTTJUMP_USE_SCREEN" = yes ] ; then
197 [ -n "$STY" ] && screen_opts=("-X" "screen")
198 screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
199 screen_opts=("${screen_opts[@]}" "-t" "$screen_window_name")
202 # Close message-stdin and open terminal-stdin instead.
203 # mutt behaves different if STDIN is no terminal
204 # TODO: Find cleaner solution (e.g. mutt command-line argument?)
206 term="/dev/$(ps -p$$ -otty=)"
210 # start mutt, open original folder and jump to the original message
211 $SCREEN "${screen_opts[@]}" $MUTT -f "$orig_maildir" -e "push \"$jump_cmd\""
215 jump_cmd="l$jump_expr
\r"
216 if [ "$MUTTJUMP_MODE" = search ] ; then
217 jump_cmd="${jump_cmd}lall
\r"
220 screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
222 if [ -n "$MUTTJUMP_SCREEN_SESSION" ] ; then
223 screen_opts=("-S" "$MUTTJUMP_SCREEN_SESSION")
224 screen_opts_str="-S $(quote "$MUTTJUMP_SCREEN_SESSION")"
227 $SCREEN "${screen_opts[@]}" -p "$screen_window_name" -X eval "select '$screen_window_name'" "stuff \"$jump_cmd\""
229 if [ $? != 0 ] ; then
230 die "You have to manually start a screen session with:
231 $SCREEN $screen_opts_str -t $(quote "$screen_window_name") $MUTT -f $(quote "$orig_maildir")"