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

Benjamin Mako Hill || Want to submit a patch?