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

Benjamin Mako Hill || Want to submit a patch?