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}
28 # If this is set to "yes", muttjump will not open a new instance of
29 # mutt, but instead jump to the original message in a running mutt
30 # (which is running in a screen session).
31 MUTTJUMP_USE_SCREEN=${MUTTJUMP_USE_SCREEN:-no}
33 # name of the screen session (screen -S ...), leave blank for none
34 MUTTJUMP_SCREEN_SESSION=${MUTTJUMP_SCREEN_SESSION:-}
36 # function to create the screen window name from the full path of the mailbox
37 if ! type -p MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE >/dev/null ; then
38 function MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE () {
45 MAIRIX=${MAIRIX:-mairix}
47 NMZMAIL=${NMZMAIL:-nmzmail}
48 SCREEN=${SCREEN:-screen}
49 FORMAIL=${FORMAIL:-formail}
50 REFORMAIL=${REFORMAIL:-reformail}
58 echo \'${1//\'/\'\\\'\'}\'
61 function is_callable () {
63 type -p $cmd >/dev/null
66 # Check command-line arguments and STDIN
67 if tty -s || [ $# -ne 0 ] ; then
71 This script calls mutt, jumping to the message given in stdin.
72 Uses a mail search engine (currently mairix, mu and nmzmail are supported),
73 which has to be configured through MUTTJUMP_INDEXER variable.
78 # check if mutt is installed
79 if ! is_callable $MUTT ; then
80 die "$MUTT is not in PATH, set MUTT variable"
83 case $MUTTJUMP_MODE in
87 die "variable MUTTJUMP_MODE must be set to \"limit\" or \"search\""
91 # search for Message-ID in STDIN
92 if is_callable $FORMAIL ; then
93 msgid=$($FORMAIL -c -z -x Message-ID | head -n1)
94 elif is_callable $REFORMAIL ; then
95 msgid=$($REFORMAIL -c -x Message-ID: | head -n1)
97 msgid=$(sed -n 's/^Message-ID:[ \t]*\(.*\)/\1/Ip' | head -n1)
99 if [ -z "$msgid" ] ; then
100 die "could not find Message-ID header in standard input"
102 msgid_clean=$(echo "$msgid" | sed 's/^<\(.*\)>$/\1/')
104 # try to locate path of message using a mail search engine
105 case $MUTTJUMP_INDEXER in
107 orig_msgfile=$($MAIRIX -r "m:$msgid_clean" | head -n1)
110 orig_msgfile=$($MU find -f l "i:$msgid_clean" |
111 grep -v "^\*\*" | head -n1)
114 orig_msgfile=$($MU find -f p "m:$msgid_clean" | head -n1)
117 nmzmail_results=$(mktemp -d)
118 echo "+message-id:$msgid" | $NMZMAIL -n 1 -r "$nmzmail_results"
119 msgfile=$(find "$nmzmail_results" -type l | head -n1)
120 orig_msgfile=$(readlink "$msgfile")
121 rm -rf "$nmzmail_results"
124 die "variable MUTTJUMP_INDEXER not set or empty"
127 die "unknown mail index program \"$MUTTJUMP_INDEXER\""
131 if [ -z "$orig_msgfile" -o ! -e "$orig_msgfile" ] ; then
132 die "no message with msgid $msgid found!"
135 # get containing maildir of $orig_msgfile
136 orig_maildir=$(dirname "$(dirname "$orig_msgfile")")
137 if [ ! -d "$orig_maildir/cur" ] ; then
138 die "directory $(quote "$orig_maildir") doesn't exist or is no Maildir"
141 jump_expr="~i'$msgid'"
143 if [ "$MUTTJUMP_USE_SCREEN" = no ] ; then
145 jump_cmd="<limit>$jump_expr<enter>"
146 if [ "$MUTTJUMP_MODE" = search ] ; then
147 jump_cmd="$jump_cmd<limit>all<enter>"
150 # Close message-stdin and open terminal-stdin instead.
151 # mutt behaves different if STDIN is no terminal
152 # TODO: Find cleaner solution (e.g. mutt command-line argument?)
154 term="/dev/$(ps -p$$ -otty=)"
157 # start mutt, open original folder and jump to the original message
158 $MUTT -f "$orig_maildir" -e "push \"$jump_cmd\""
162 jump_cmd="l$jump_expr
\r"
163 if [ "$MUTTJUMP_MODE" = search ] ; then
164 jump_cmd="${jump_cmd}lall
\r"
167 screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
168 declare -a screen_opts
169 if [ -n "$MUTTJUMP_SCREEN_SESSION" ] ; then
170 screen_opts=("-S" "$MUTTJUMP_SCREEN_SESSION")
171 screen_opts_str="-S $(quote "$MUTTJUMP_SCREEN_SESSION")"
174 $SCREEN "${screen_opts[@]}" -p "$screen_window_name" -X eval "select '$screen_window_name'" "stuff \"$jump_cmd\""
176 if [ $? != 0 ] ; then
177 die "You have to manually start a screen session with:
178 $SCREEN $screen_opts_str -t $(quote "$screen_window_name") $MUTT -f $(quote "$orig_maildir")"