Added support for using notmuch as an external indexer.
[muttjump] / muttjump
index 436f8039d2ee07c0ecd506ca204deb89add34b52..37dd0c2b016ab9f33a70a4a51d675685ae6bec49 100755 (executable)
--- a/muttjump
+++ b/muttjump
@@ -3,8 +3,8 @@
 
 # muttjump
 #
-# This script makes mail indexers (like mairix, mu or nmzmail) together with
-# mutt more useful.
+# This script makes mail indexers (like mairix, mu, nmzmail, or
+# notmuch) together with mutt more useful.
 #
 # These search engines usually create a virtual maildir containing symbolic
 # links to the original mails, which can be browsed using mutt.
 #
 # macro generic ,j "<enter-command>push <pipe-message>muttjump<enter><enter>" "jump to original message"
 
-# one of: mairix, mu, mu-old (mu < 0.7) and nmzmail
+# one of: mairix, mairix-git, mu, mu-old (mu < 0.7), nmzmail or notmuch (>0.5)
 MUTTJUMP_INDEXER=${MUTTJUMP_INDEXER:-}
 
 # "limit" or "search" (default)
 MUTTJUMP_MODE=${MUTTJUMP_MODE:-search}
 
-# "yes" or "no"
-# If this is set to "yes", muttjump will not open a new instance of
-# mutt, but instead jump to the original message in a running mutt
-# (which is running in a screen session).
-MUTTJUMP_USE_SCREEN=${MUTTJUMP_USE_SCREEN:-no}
+# "yes", "no" or "auto"
+# If set to "auto", the $STY environment variable is used for auto detection.
+MUTTJUMP_USE_SCREEN=${MUTTJUMP_USE_SCREEN:-auto}
 
-# name of the screen session (screen -S ...), leave blank for none
-MUTTJUMP_SCREEN_SESSION=${MUTTJUMP_SCREEN_SESSION:-}
+# "yes" or "no"
+# Set this to "yes" if you want exactly one screen window (with mutt running
+# inside) per mailbox. With new versions of screen (> 4.00) it is possible
+# to do that automatically, with old versions you have to start them
+# manually! Setting this to yes implies MUTTJUMP_USE_SCREEN=yes.
+MUTTJUMP_MULTI_SCREEN_MODE=${MUTTJUMP_MULTI_SCREEN_MODE:-no}
 
 # function to create the screen window name from the full path of the mailbox
-if ! type -p MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE >/dev/null ; then
+if ! type MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE >/dev/null 2>&1 ; then
     function MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE () {
         basename "$1"
     }
 fi
 
+# Version of GNU screen
+# Since 4.01.00devel (commit 98b6b41) backslash handling for -X was altered.
+MUTTJUMP_SCREEN_VERSION=${MUTTJUMP_SCREEN_VERSION:-auto}
+
 # program paths
 MUTT=${MUTT:-mutt}
 MAIRIX=${MAIRIX:-mairix}
 MU=${MU:-mu}
 NMZMAIL=${NMZMAIL:-nmzmail}
+NOTMUCH=${NOTMUCH:-notmuch}
 SCREEN=${SCREEN:-screen}
 FORMAIL=${FORMAIL:-formail}
 REFORMAIL=${REFORMAIL:-reformail}
+WHIPTAIL=${WHIPTAIL:-whiptail --noitem}
+DIALOG=${DIALOG:-dialog}
 
 function die () {
     echo -e >&2 "$0: $1"
@@ -58,23 +67,78 @@ function quote () {
     echo \'${1//\'/\'\\\'\'}\'
 }
 
-# Check command-line arguments and STDIN
-if tty -s || [ $# -ne 0 ] ; then
+function quote_regexp () {
+    echo "$1" | sed 's/[][\^.$|)(*+?}{]/\\&/g'
+}
+
+function is_callable () {
+    type $1 >/dev/null 2>&1
+}
+
+function wc_L () {
+    awk '{ y = length(); if (y > x) x = y } END { print x }'
+}
+
+# Check if screen is new enough to parse -X (commit 98b6b41) and support
+# -Q (commit 8147d08). It (falsely) assumes that screen version > 4.00
+# does this. Distributions that use git versions are likely to also keep
+# them up to date.
+function is_new_screen () {
+    sv=$MUTTJUMP_SCREEN_VERSION
+    if [ "$sv" = auto ] ; then
+        sv=$($SCREEN --version | awk '{print $3}')
+    fi
+    echo "$sv" | grep -vq "^\(4\.00\|[0-3]\.\)"
+}
+
+function usage () {
     cat >&2 <<END
-Usage: $0 < msg
+Usage: muttjump msgid
+       cat msg | muttjump [-R]
 
-This script calls mutt, jumping to the message given in stdin.
-Uses a mail search engine (currently mairix, mu and nmzmail are supported),
+This script calls mutt, jumping to the message given in stdin or to the message
+identified by "msgid".  With "-R", it jumps to the message being replied to.
+It uses a mail search engine (currently mairix, mu and nmzmail are supported),
 which has to be configured through MUTTJUMP_INDEXER variable.
 END
     exit 1
+}
+
+function reopen_tty () {
+    # Close message-stdin and open terminal-stdin instead.
+    # mutt behaves different if STDIN is no terminal
+    # TODO: Find cleaner solution (e.g. mutt command-line argument?)
+    exec 0<&-
+    term="/dev/$(ps -p$$ -otty=)"
+    exec < $term
+}
+
+
+# Check command-line arguments and STDIN
+search_header="Message-ID"
+if tty -s ; then
+    [ $# -ne 1 ] && usage
+else
+    [ $# -gt 1 ] && usage
+    if [ $# -eq 1 ] ; then
+        [ "$1" != "-R" ] && usage
+        search_header="In-Reply-To"
+        shift
+    fi
 fi
 
 # check if mutt is installed
-if ! type -p $MUTT >/dev/null ; then
+if ! is_callable $MUTT ; then
     die "$MUTT is not in PATH, set MUTT variable"
 fi
 
+DIALOG_PROG=""
+if is_callable $WHIPTAIL ; then
+    DIALOG_PROG=$WHIPTAIL
+elif is_callable $DIALOG ; then
+    DIALOG_PROG=$DIALOG
+fi
+
 case $MUTTJUMP_MODE in
     limit|search)
         ;;
@@ -84,37 +148,53 @@ case $MUTTJUMP_MODE in
 esac
 
 # search for Message-ID in STDIN
-if type -p $FORMAIL >/dev/null ; then
-    msgid=$($FORMAIL -c -z -x Message-ID | head -n1)
-elif type -p $REFORMAIL >/dev/null ; then
-    msgid=$($REFORMAIL -c -x Message-ID: | head -n1)
+if [ $# -eq 1 ] ; then
+    msgid=$1
+elif is_callable $FORMAIL ; then
+    msgid=$($FORMAIL -c -z -x $search_header | head -n1)
+elif is_callable $REFORMAIL ; then
+    msgid=$($REFORMAIL -c -x $search_header: | head -n1)
 else
-    msgid=$(sed -n 's/^Message-ID: \(.*\)/\1/Ip' | head -n1)
+    msgid=$(sed -n 's/^'$search_header':[ \t]*\(.*\)/\1/Ip' | head -n1)
 fi
 if [ -z "$msgid" ] ; then
-    die "could not find Message-ID header in standard input"
+    die "could not find $search_header header in standard input"
 fi
-msgid_clean=$(echo "$msgid" | sed 's/^<\(.*\)>$/\1/')
+msgid_quoted=$(quote_regexp "$msgid")
+msgid_clean=$(echo "$msgid" | sed -e 's/^<//' -e 's/>$//')
 
 # try to locate path of message using a mail search engine
 case $MUTTJUMP_INDEXER in
+    mairix-git)
+        orig_msgfiles=$($MAIRIX -r "m:$msgid_clean")
+        ;;
     mairix)
-        orig_msgfile=$($MAIRIX -r "m:$msgid_clean" | head -n1)
+        msgid_mairix=$msgid_clean
+        # mairix can't quote special characters (~,/=^) in search words. As a
+        # workaround, split Message-ID in 31-character long AND-linked
+        # substrings.
+        if echo "$msgid_clean" | grep -q '[~,/=^]' ; then
+            msgid_mairix=$(echo "$msgid_clean" | sed -e 's/[~,/=^]\+/ /g' \
+                -e 's/[^ ]\{31\}/& /g' -e 's/^ \+//g' -e 's/ *$/=/g' \
+                -e 's/ /=,/g')
+        fi
+        orig_msgfiles=$($MAIRIX -r "m:$msgid_mairix")
         ;;
     mu)
-        orig_msgfile=$($MU find -f l "i:$msgid_clean" |
-            grep -v "^\*\*" | head -n1)
+        orig_msgfiles=$($MU find -f l "i:$msgid_clean" | grep -v "^\*\*")
         ;;
     mu-old)
-        orig_msgfile=$($MU find -f p "m:$msgid_clean" | head -n1)
+        orig_msgfiles=$($MU find -f p "m:$msgid_clean")
         ;;
     nmzmail)
         nmzmail_results=$(mktemp -d)
-        echo "+message-id:$msgid" | $NMZMAIL -n 1 -r "$nmzmail_results"
-        msgfile=$(find "$nmzmail_results" -type l | head -n1)
-        orig_msgfile=$(readlink "$msgfile")
+        echo "+message-id:/^$msgid_quoted$/" | $NMZMAIL -r "$nmzmail_results"
+        orig_msgfiles=$(find "$nmzmail_results" -type l -exec readlink {} \;)
         rm -rf "$nmzmail_results"
         ;;
+    notmuch)
+       orig_msgfiles=$($NOTMUCH search --output=files "id:$msgid_clean")
+       ;;
     "")
         die "variable MUTTJUMP_INDEXER not set or empty"
         ;;
@@ -123,54 +203,78 @@ case $MUTTJUMP_INDEXER in
         ;;
 esac
 
-if [ -z "$orig_msgfile" -o ! -e "$orig_msgfile" ] ; then
+if [ -z "$orig_msgfiles" ] ; then
     die "no message with msgid $msgid found!"
 fi
 
-# get containing maildir of $orig_msgfile
-orig_maildir=$(dirname "$(dirname "$orig_msgfile")")
+count=$(echo "$orig_msgfiles" | wc -l)
+if [ $count -gt 1 -a -n "$DIALOG_PROG" ] ; then
+    choices=$(echo "$orig_msgfiles" | while read line ;
+        do dirname "$(dirname "$line")" ; echo . ; done)
+    maxwidth=$(echo "$choices" | wc_L)
+    orig_maildir=$($DIALOG_PROG --clear --title "More than one mailbox found" \
+        --menu "Select mailbox:" $((count+8)) $((maxwidth+16)) \
+        $count $choices 3>&2 2>&1 1>&3-)
+elif [ $count -gt 1 ] ; then
+    choices=()
+    while read line ; do
+        choices=("${choices[@]}" "$(dirname "$(dirname "$line")")")
+    done <<<"$orig_msgfiles"
+    echo "More than one mailbox found"
+    echo "Select mailbox:"
+    select md in "${choices[@]}" ; do
+        orig_maildir=$md
+        break
+    done <> /dev/tty
+else
+    orig_msgfile=$orig_msgfiles
+    # get containing maildir of $orig_msgfile
+    orig_maildir=$(dirname "$(dirname "$orig_msgfile")")
+fi
+
 if [ ! -d "$orig_maildir/cur" ] ; then
     die "directory $(quote "$orig_maildir") doesn't exist or is no Maildir"
 fi
 
-jump_expr="~i'$msgid'"
-
-if [ "$MUTTJUMP_USE_SCREEN" = no ] ; then
-
-    jump_cmd="<$MUTTJUMP_MODE>$jump_expr<enter>"
-
-    # Close message-stdin and open terminal-stdin instead.
-    # mutt behaves different if STDIN is no terminal
-    # TODO: Find cleaner solution (e.g. mutt command-line argument?)
-    exec 0<&-
-    term="/dev/$(ps -p$$ --no-heading | awk '{print $2}')"
-    exec < $term
-
-    # start mutt, open original folder and jump to the original message
-    $MUTT -f "$orig_maildir" -e "push \"$jump_cmd\""
-
-else
+msgid_mutt=$(quote_regexp "$msgid_quoted")
+jump_expr="~i'$msgid_mutt'"
+jump_cmd="<limit>$jump_expr<enter>"
+if [ "$MUTTJUMP_MODE" = search ] ; then
+    jump_cmd="$jump_cmd<limit>all<enter>"
+fi
 
-    case $MUTTJUMP_MODE in
-        limit)
-            jump_cmd="l$jump_expr"
-            ;;
-        search)
-            jump_cmd="/$jump_expr"
-            ;;
-    esac
+if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = yes ] ||
+        [ "$MUTTJUMP_USE_SCREEN" = auto -a -n "$STY" ] ; then
+    MUTTJUMP_USE_SCREEN=yes
+fi
 
-    screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
-    declare -a screen_opts
-    if [ -n "$MUTTJUMP_SCREEN_SESSION" ] ; then
-        screen_opts=("-S" "$MUTTJUMP_SCREEN_SESSION")
-        screen_opts_str="-S $(quote "$MUTTJUMP_SCREEN_SESSION")"
+screen_opts=()
+screen_query_arg=""
+if [ "$MUTTJUMP_USE_SCREEN" = yes ] ; then
+    if [ -n "$STY" ] ; then
+        screen_opts=("-X" "screen")
+        if is_new_screen ; then
+            jump_cmd=${jump_cmd/\\/\\\\\\}
+            screen_query_arg="-Q"
+        fi
+    else
+        reopen_tty
     fi
+    screen_window_name=$(MUTTJUMP_SCREEN_WINDOW_NAME_MANGLE "$orig_maildir")
+    screen_opts=("${screen_opts[@]}" "-t" "$screen_window_name")
+else
+    SCREEN=""
+    reopen_tty
+fi
 
-    $SCREEN "${screen_opts[@]}" -p "$screen_window_name" -X eval "select '$screen_window_name'" "stuff \"$jump_cmd\r\""
-
-    if [ $? != 0 ] ; then
-        die "You have to manually start a screen session with:
-$SCREEN $screen_opts_str -t $(quote "$screen_window_name") $MUTT -f $(quote "$orig_maildir")"
+if [ "$MUTTJUMP_MULTI_SCREEN_MODE" = yes -a -n "$STY" ] ; then
+    $SCREEN -X -p "$screen_window_name" $screen_query_arg \
+            select "$screen_window_name" >/dev/null
+    if [ $? = 0 ] ; then
+        $SCREEN -X -p "$screen_window_name" stuff ":push \"$jump_cmd\"\r"
+        exit 0
     fi
 fi
+
+# start mutt, open original folder and jump to the original message
+$SCREEN "${screen_opts[@]}" $MUTT -f "$orig_maildir" -e "push \"$jump_cmd\""

Benjamin Mako Hill || Want to submit a patch?