use dialog to pick maildir if multiple exist
[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 # name of the screen session (screen -S ...), leave blank for none
40 MUTTJUMP_SCREEN_SESSION=${MUTTJUMP_SCREEN_SESSION:-}
41
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 () {
45         basename "$1"
46     }
47 fi
48
49 # program paths
50 MUTT=${MUTT:-mutt}
51 MAIRIX=${MAIRIX:-mairix}
52 MU=${MU:-mu}
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}
59
60 function die () {
61     echo -e >&2 "$0: $1"
62     exit 1
63 }
64
65 function quote () {
66     echo \'${1//\'/\'\\\'\'}\'
67 }
68
69 function is_callable () {
70     type $1 >/dev/null 2>&1
71 }
72
73 # Check command-line arguments and STDIN
74 if tty -s || [ $# -ne 0 ] ; then
75     cat >&2 <<END
76 Usage: $0 < msg
77
78 This script calls mutt, jumping to the message given in stdin.
79 Uses a mail search engine (currently mairix, mu and nmzmail are supported),
80 which has to be configured through MUTTJUMP_INDEXER variable.
81 END
82     exit 1
83 fi
84
85 # check if mutt is installed
86 if ! is_callable $MUTT ; then
87     die "$MUTT is not in PATH, set MUTT variable"
88 fi
89
90 DIALOG_PROG=""
91 if is_callable $WHIPTAIL ; then
92     DIALOG_PROG=$WHIPTAIL
93 elif is_callable $DIALOG ; then
94     DIALOG_PROG=$DIALOG
95 fi
96
97 case $MUTTJUMP_MODE in
98     limit|search)
99         ;;
100     *)
101         die "variable MUTTJUMP_MODE must be set to \"limit\" or \"search\""
102         ;;
103 esac
104
105 # search for Message-ID in STDIN
106 if is_callable $FORMAIL ; then
107     msgid=$($FORMAIL -c -z -x Message-ID | head -n1)
108 elif is_callable $REFORMAIL ; then
109     msgid=$($REFORMAIL -c -x Message-ID: | head -n1)
110 else
111     msgid=$(sed -n 's/^Message-ID:[ \t]*\(.*\)/\1/Ip' | head -n1)
112 fi
113 if [ -z "$msgid" ] ; then
114     die "could not find Message-ID header in standard input"
115 fi
116 msgid_clean=$(echo "$msgid" | sed 's/^<\(.*\)>$/\1/')
117
118 # try to locate path of message using a mail search engine
119 case $MUTTJUMP_INDEXER in
120     mairix)
121         orig_msgfiles=$($MAIRIX -r "m:$msgid_clean")
122         ;;
123     mu)
124         orig_msgfiles=$($MU find -f l "i:$msgid_clean" | grep -v "^\*\*")
125         ;;
126     mu-old)
127         orig_msgfiles=$($MU find -f p "m:$msgid_clean")
128         ;;
129     nmzmail)
130         nmzmail_results=$(mktemp -d)
131         echo "+message-id:$msgid" | $NMZMAIL -r "$nmzmail_results"
132         orig_msgfiles=$(find "$nmzmail_results" -type l -exec readlink {} \;)
133         rm -rf "$nmzmail_results"
134         ;;
135     "")
136         die "variable MUTTJUMP_INDEXER not set or empty"
137         ;;
138     *)
139         die "unknown mail index program \"$MUTTJUMP_INDEXER\""
140         ;;
141 esac
142
143 if [ -z "$orig_msgfiles" ] ; then
144     die "no message with msgid $msgid found!"
145 fi
146
147 count=$(echo "$orig_msgfiles" | wc -l)
148 if [ $count -gt 1 -a -n "$DIALOG_PROG" ] ; then
149     choices=$(echo "$orig_msgfiles" | while read line ;
150         do echo -e "$(dirname "$(dirname "$line")")\n." ; done)
151     maxwidth=$(echo "$choices" | wc -L)
152     orig_maildir=$($DIALOG_PROG --clear --title "More than one mailbox found" \
153         --menu "Select mailbox:" $((count+8)) $((maxwidth+16)) \
154         $count $choices 3>&2 2>&1 1>&3-)
155 else
156     orig_msgfile=$(echo "$orig_msgfiles" | head -n1)
157     # get containing maildir of $orig_msgfile
158     orig_maildir=$(dirname "$(dirname "$orig_msgfile")")
159 fi
160
161 if [ ! -d "$orig_maildir/cur" ] ; then
162     die "directory $(quote "$orig_maildir") doesn't exist or is no Maildir"
163 fi
164
165 jump_expr="~i'$msgid'"
166
167 if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = no ] ; then
168
169     jump_cmd="<limit>$jump_expr<enter>"
170     if [ "$MUTTJUMP_MODE" = search ] ; then
171         jump_cmd="$jump_cmd<limit>all<enter>"
172     fi
173
174     if [ "$MUTTJUMP_USE_SCREEN" = auto -a -n "$STY" ] ; then
175         MUTTJUMP_USE_SCREEN=yes
176     fi
177
178     screen_opts=()
179     if [ "$MUTTJUMP_USE_SCREEN" = yes ] ; then
180         [ -n "$STY" ] && screen_opts=("-X" "screen")
181         screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
182         screen_opts=("${screen_opts[@]}" "-t" "$screen_window_name")
183     else
184         SCREEN=""
185         # Close message-stdin and open terminal-stdin instead.
186         # mutt behaves different if STDIN is no terminal
187         # TODO: Find cleaner solution (e.g. mutt command-line argument?)
188         exec 0<&-
189         term="/dev/$(ps -p$$ -otty=)"
190         exec < $term
191     fi
192
193     # start mutt, open original folder and jump to the original message
194     $SCREEN "${screen_opts[@]}" $MUTT -f "$orig_maildir" -e "push \"$jump_cmd\""
195
196 else
197
198     jump_cmd="l$jump_expr\r"
199     if [ "$MUTTJUMP_MODE" = search ] ; then
200         jump_cmd="${jump_cmd}lall\r"
201     fi
202
203     screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
204     screen_opts=()
205     if [ -n "$MUTTJUMP_SCREEN_SESSION" ] ; then
206         screen_opts=("-S" "$MUTTJUMP_SCREEN_SESSION")
207         screen_opts_str="-S $(quote "$MUTTJUMP_SCREEN_SESSION")"
208     fi
209
210     $SCREEN "${screen_opts[@]}" -p "$screen_window_name" -X eval "select '$screen_window_name'" "stuff \"$jump_cmd\""
211
212     if [ $? != 0 ] ; then
213         die "You have to manually start a screen session with:
214 $SCREEN $screen_opts_str -t $(quote "$screen_window_name") $MUTT -f $(quote "$orig_maildir")"
215     fi
216 fi

Benjamin Mako Hill || Want to submit a patch?