2062ac55672289549a3bfb4d350d63911e3bd1e1
[muttjump] / muttjump
1 #!/bin/bash
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", "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 # 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
36 #            for this to work!
37 MUTTJUMP_MULTI_SCREEN_MODE=${MUTTJUMP_MULTI_SCREEN_MODE:-no}
38
39 # function to create the screen window name from the full path of the mailbox
40 if ! type MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE >/dev/null 2>&1 ; then
41     function MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE () {
42         basename "$1"
43     }
44 fi
45
46 # Version of GNU screen
47 # Since 4.01.00 (commit d87a0d8) backslash handling was altered.
48 MUTTJUMP_SCREEN_VERSION=${MUTTJUMP_SCREEN_VERSION:-auto}
49
50 # program paths
51 MUTT=${MUTT:-mutt}
52 MAIRIX=${MAIRIX:-mairix}
53 MU=${MU:-mu}
54 NMZMAIL=${NMZMAIL:-nmzmail}
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 function is_new_screen () {
83     sv=$MUTTJUMP_SCREEN_VERSION
84     if [ "$sv" = auto ] ; then
85         sv=$($SCREEN --version | awk '{print $3}')
86     fi
87     echo "$sv" | grep -vq "^\(4\.00\|[0-3]\.\)"
88 }
89
90 function usage () {
91     cat >&2 <<END
92 Usage: muttjump msgid
93        cat msg | muttjump [-R]
94
95 This script calls mutt, jumping to the message given in stdin or to the message
96 identified by "msgid".  With "-R", it jumps to the message being replied to.
97 It uses a mail search engine (currently mairix, mu and nmzmail are supported),
98 which has to be configured through MUTTJUMP_INDEXER variable.
99 END
100     exit 1
101 }
102
103 function reopen_tty () {
104     # Close message-stdin and open terminal-stdin instead.
105     # mutt behaves different if STDIN is no terminal
106     # TODO: Find cleaner solution (e.g. mutt command-line argument?)
107     exec 0<&-
108     term="/dev/$(ps -p$$ -otty=)"
109     exec < $term
110 }
111
112
113 # Check command-line arguments and STDIN
114 search_header="Message-ID"
115 if tty -s ; then
116     [ $# -ne 1 ] && usage
117 else
118     [ $# -gt 1 ] && usage
119     if [ $# -eq 1 ] ; then
120         [ "$1" != "-R" ] && usage
121         search_header="In-Reply-To"
122         shift
123     fi
124 fi
125
126 # check if mutt is installed
127 if ! is_callable $MUTT ; then
128     die "$MUTT is not in PATH, set MUTT variable"
129 fi
130
131 DIALOG_PROG=""
132 if is_callable $WHIPTAIL ; then
133     DIALOG_PROG=$WHIPTAIL
134 elif is_callable $DIALOG ; then
135     DIALOG_PROG=$DIALOG
136 fi
137
138 case $MUTTJUMP_MODE in
139     limit|search)
140         ;;
141     *)
142         die "variable MUTTJUMP_MODE must be set to \"limit\" or \"search\""
143         ;;
144 esac
145
146 # search for Message-ID in STDIN
147 if [ $# -eq 1 ] ; then
148     msgid=$1
149 elif is_callable $FORMAIL ; then
150     msgid=$($FORMAIL -c -z -x $search_header | head -n1)
151 elif is_callable $REFORMAIL ; then
152     msgid=$($REFORMAIL -c -x $search_header: | head -n1)
153 else
154     msgid=$(sed -n 's/^'$search_header':[ \t]*\(.*\)/\1/Ip' | head -n1)
155 fi
156 if [ -z "$msgid" ] ; then
157     die "could not find $search_header header in standard input"
158 fi
159 msgid_quoted=$(quote_regexp "$msgid")
160 msgid_clean=$(echo "$msgid" | sed -e 's/^<//' -e 's/>$//')
161
162 # try to locate path of message using a mail search engine
163 case $MUTTJUMP_INDEXER in
164     mairix)
165         msgid_mairix=$msgid_clean
166         # mairix can't quote special characters (~,/=^) in search words. As a
167         # workaround, split Message-ID in 31-character long AND-linked
168         # substrings.
169         if echo "$msgid_clean" | grep -q '[~,/=^]' ; then
170             msgid_mairix=$(echo "$msgid_clean" | sed -e 's/[~,/=^]\+/ /g' \
171                 -e 's/[^ ]\{31\}/& /g' -e 's/^ \+//g' -e 's/ *$/=/g' \
172                 -e 's/ /=,/g')
173         fi
174         orig_msgfiles=$($MAIRIX -r "m:$msgid_mairix")
175         ;;
176     mu)
177         orig_msgfiles=$($MU find -f l "i:$msgid_clean" | grep -v "^\*\*")
178         ;;
179     mu-old)
180         orig_msgfiles=$($MU find -f p "m:$msgid_clean")
181         ;;
182     nmzmail)
183         nmzmail_results=$(mktemp -d)
184         echo "+message-id:/^$msgid_quoted$/" | $NMZMAIL -r "$nmzmail_results"
185         orig_msgfiles=$(find "$nmzmail_results" -type l -exec readlink {} \;)
186         rm -rf "$nmzmail_results"
187         ;;
188     "")
189         die "variable MUTTJUMP_INDEXER not set or empty"
190         ;;
191     *)
192         die "unknown mail index program \"$MUTTJUMP_INDEXER\""
193         ;;
194 esac
195
196 if [ -z "$orig_msgfiles" ] ; then
197     die "no message with msgid $msgid found!"
198 fi
199
200 count=$(echo "$orig_msgfiles" | wc -l)
201 if [ $count -gt 1 -a -n "$DIALOG_PROG" ] ; then
202     choices=$(echo "$orig_msgfiles" | while read line ;
203         do dirname "$(dirname "$line")" ; echo . ; done)
204     maxwidth=$(echo "$choices" | wc_L)
205     orig_maildir=$($DIALOG_PROG --clear --title "More than one mailbox found" \
206         --menu "Select mailbox:" $((count+8)) $((maxwidth+16)) \
207         $count $choices 3>&2 2>&1 1>&3-)
208 elif [ $count -gt 1 ] ; then
209     choices=()
210     while read line ; do
211         choices=("${choices[@]}" "$(dirname "$(dirname "$line")")")
212     done <<<"$orig_msgfiles"
213     echo "More than one mailbox found"
214     echo "Select mailbox:"
215     select md in "${choices[@]}" ; do
216         orig_maildir=$md
217         break
218     done <> /dev/tty
219 else
220     orig_msgfile=$orig_msgfiles
221     # get containing maildir of $orig_msgfile
222     orig_maildir=$(dirname "$(dirname "$orig_msgfile")")
223 fi
224
225 if [ ! -d "$orig_maildir/cur" ] ; then
226     die "directory $(quote "$orig_maildir") doesn't exist or is no Maildir"
227 fi
228
229 msgid_mutt=$(quote_regexp "$msgid_quoted")
230 jump_expr="~i'$msgid_mutt'"
231 jump_cmd="<limit>$jump_expr<enter>"
232 if [ "$MUTTJUMP_MODE" = search ] ; then
233     jump_cmd="$jump_cmd<limit>all<enter>"
234 fi
235
236 if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = yes ] ||
237         [ "$MUTTJUMP_USE_SCREEN" = auto -a -n "$STY" ] ; then
238     MUTTJUMP_USE_SCREEN=yes
239 fi
240
241 screen_opts=()
242 if [ "$MUTTJUMP_USE_SCREEN" = yes ] ; then
243     if [ -n "$STY" ] ; then
244         screen_opts=("-X" "screen")
245         if is_new_screen ; then
246             jump_cmd=${jump_cmd/\\/\\\\\\}
247         fi
248     else
249         reopen_tty
250     fi
251     screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
252     screen_opts=("${screen_opts[@]}" "-t" "$screen_window_name")
253 else
254     SCREEN=""
255     reopen_tty
256 fi
257
258 if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = yes -a -n "$STY" ] ; then
259     if is_new_screen ; then
260         $SCREEN -X -p "$screen_window_name" -Q select "$screen_window_name" >/dev/null
261     fi
262     if [ $? = 0 ] ; then
263         $SCREEN -X -p "$screen_window_name" stuff ":push \"$jump_cmd\"\r"
264         exit 0
265     fi
266 fi
267
268 # start mutt, open original folder and jump to the original message
269 $SCREEN "${screen_opts[@]}" $MUTT -f "$orig_maildir" -e "push \"$jump_cmd\""

Benjamin Mako Hill || Want to submit a patch?