351e3ac17019c3eb3d83dd9890f972acf2606de3
[muttjump] / muttjump
1 #!/bin/bash -x
2 # written by Johannes Weißl
3
4 # muttjump
5 #
6 # This script makes mail indexers (like mairix, mu or nmzmail) together with
7 # 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, mu, mu-old (mu < 0.7) and nmzmail
22 MUTTJUMP_INDEXER=${MUTTJUMP_INDEXER:-}
23
24 # "limit" or "search" (default)
25 MUTTJUMP_MODE=${MUTTJUMP_MODE:-search}
26
27 # "yes" or "no"
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}
32
33 # name of the screen session (screen -S ...), leave blank for none
34 MUTTJUMP_SCREEN_SESSION=${MUTTJUMP_SCREEN_SESSION:-}
35
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 () {
39         basename "$1"
40     }
41 fi
42
43 # program paths
44 MUTT=${MUTT:-mutt}
45 MAIRIX=${MAIRIX:-mairix}
46 MU=${MU:-mu}
47 NMZMAIL=${NMZMAIL:-nmzmail}
48 SCREEN=${SCREEN:-screen}
49 FORMAIL=${FORMAIL:-formail}
50 REFORMAIL=${REFORMAIL:-reformail}
51
52 function die () {
53     echo -e >&2 "$0: $1"
54     exit 1
55 }
56
57 function quote () {
58     echo \'${1//\'/\'\\\'\'}\'
59 }
60
61 # Check command-line arguments and STDIN
62 if tty -s || [ $# -ne 0 ] ; then
63     cat >&2 <<END
64 Usage: $0 < msg
65
66 This script calls mutt, jumping to the message given in stdin.
67 Uses a mail search engine (currently mairix, mu and nmzmail are supported),
68 which has to be configured through MUTTJUMP_INDEXER variable.
69 END
70     exit 1
71 fi
72
73 # check if mutt is installed
74 if ! type -p $MUTT >/dev/null ; then
75     die "$MUTT is not in PATH, set MUTT variable"
76 fi
77
78 case $MUTTJUMP_MODE in
79     limit|search)
80         ;;
81     *)
82         die "variable MUTTJUMP_MODE must be set to \"limit\" or \"search\""
83         ;;
84 esac
85
86 # search for Message-ID in STDIN
87 if type -p $FORMAIL >/dev/null ; then
88     msgid=$($FORMAIL -c -z -x Message-ID | head -n1)
89 elif type -p $REFORMAIL >/dev/null ; then
90     msgid=$($REFORMAIL -c -x Message-ID: | head -n1)
91 else
92     msgid=$(sed -n 's/^Message-ID: \(.*\)/\1/Ip' | head -n1)
93 fi
94 if [ -z "$msgid" ] ; then
95     die "could not find Message-ID header in standard input"
96 fi
97 msgid_clean=$(echo "$msgid" | sed 's/^<\(.*\)>$/\1/')
98
99 # try to locate path of message using a mail search engine
100 case $MUTTJUMP_INDEXER in
101     mairix)
102         orig_msgfile=$($MAIRIX -r "m:$msgid_clean" | head -n1)
103         ;;
104     mu)
105         orig_msgfile=$($MU find -f l "i:$msgid_clean" |
106             grep -v "^\*\*" | head -n1)
107         ;;
108     mu-old)
109         orig_msgfile=$($MU find -f p "m:$msgid_clean" | head -n1)
110         ;;
111     nmzmail)
112         nmzmail_results=$(mktemp -d)
113         echo "+message-id:$msgid" | $NMZMAIL -n 1 -r "$nmzmail_results"
114         msgfile=$(find "$nmzmail_results" -type l | head -n1)
115         orig_msgfile=$(readlink "$msgfile")
116         rm -rf "$nmzmail_results"
117         ;;
118     "")
119         die "variable MUTTJUMP_INDEXER not set or empty"
120         ;;
121     *)
122         die "unknown mail index program \"$MUTTJUMP_INDEXER\""
123         ;;
124 esac
125
126 if [ -z "$orig_msgfile" -o ! -e "$orig_msgfile" ] ; then
127     die "no message with msgid $msgid found!"
128 fi
129
130 # get containing maildir of $orig_msgfile
131 orig_maildir=$(dirname "$(dirname "$orig_msgfile")")
132 if [ ! -d "$orig_maildir/cur" ] ; then
133     die "directory $(quote "$orig_maildir") doesn't exist or is no Maildir"
134 fi
135
136 jump_expr="~i'$msgid'"
137
138 if [ "$MUTTJUMP_USE_SCREEN" = no ] ; then
139
140     jump_cmd="<$MUTTJUMP_MODE>$jump_expr<enter>"
141
142     # Close message-stdin and open terminal-stdin instead.
143     # mutt behaves different if STDIN is no terminal
144     # TODO: Find cleaner solution (e.g. mutt command-line argument?)
145     exec 0<&-
146     term="/dev/$(ps -p$$ --no-heading | awk '{print $2}')"
147     exec < $term
148
149     # start mutt, open original folder and jump to the original message
150     $MUTT -f "$orig_maildir" -e "push \"$jump_cmd\""
151
152 else
153
154     case $MUTTJUMP_MODE in
155         limit)
156             jump_cmd="l$jump_expr"
157             ;;
158         search)
159             jump_cmd="/$jump_expr"
160             ;;
161     esac
162
163     screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
164     declare -a screen_opts
165     if [ -n "$MUTTJUMP_SCREEN_SESSION" ] ; then
166         screen_opts=("-S" "$MUTTJUMP_SCREEN_SESSION")
167         screen_opts_str="-S $(quote "$MUTTJUMP_SCREEN_SESSION")"
168     fi
169
170     $SCREEN "${screen_opts[@]}" -p "$screen_window_name" -X eval "select '$screen_window_name'" "stuff \"$jump_cmd\r\""
171
172     if [ $? != 0 ] ; then
173         die "You have to manually start a screen session with:
174 $SCREEN $screen_opts_str -t $(quote "$screen_window_name") $MUTT -f $(quote "$orig_maildir")"
175     fi
176 fi

Benjamin Mako Hill || Want to submit a patch?