2 # written by Johannes Weißl
6 # This is a fallback solution for muttjump which doesn't use any mail
7 # indexer to find the original mail (which is of course way slower in
10 # This requires two folder-hooks in .muttrc:
12 # # hack: set my_cwd to current folder path, abusing $record
13 # # todo: better solution?
14 # folder-hook . 'set my_backup_record=$record ; set record=^ ; set my_cwd=$record ; set record=$my_backup_record'
16 # # set macro for jumping to original message
17 # # needs to be set for every folder after my_cwd (see above) has been set
18 # # todo: better solution?
19 # folder-hook . 'macro generic ,J "<enter-command>push <pipe-message>\"muttjump_fallback $my_cwd $folder\"<enter><enter>" "jump to original message"'
29 # Check command-line arguments and STDIN
30 if tty -s || [ $# -gt 2 ] || [ $# -lt 1 ] ; then
32 Usage: $0 virtual-maildir [folder] < msg
34 This script calls mutt, jumping to the message given in stdin.
35 It uses grep to search for the messages in "virtual-maildir" (a maildir
36 containing only symbolic links), and finds out the target through readlink.
37 If "virtual-maildir" contains shortcuts like '=', "folder" can be given for
43 # check if mutt is installed
44 if ! type -p $MUTT >/dev/null ; then
45 die "$MUTT is not in PATH, set MUTT variable"
48 # search for Message-ID in STDIN
50 msgid_line=$(tee "$tmpfile" | grep -i '^Message-ID: ' | head -n1)
51 msgid=$(echo "$msgid_line" | sed -n 's/^[^:]*: \(.*\)/\1/p')
52 msgsize=$(wc -c "$tmpfile")
55 if [ -z "$msgid" ] ; then
56 die "could not find Message-ID header in standard input"
58 msgid_clean=$(echo "$msgid" | sed 's/^<\(.*\)>$/\1/')
62 # If $folder is given as second argument, replace "+" or "=" in $maildir
63 if [ $# -gt 1 ] ; then
65 maildir=$(echo "$maildir" | sed "s@^[+=]@$folder/@")
68 if [ ! -d "$maildir/cur" ] ; then
69 die "directory \"$maildir\" doesn't exist or is no Maildir"
72 # grep for Message-ID in the current Maildir
73 # TODO: This is definitely not what we want, but there seems to be no way
74 # to pass the filename of the currently selected mail to an external
75 # program, as this is dependent on the folder format.
76 num_files=$(ls -1 "$maildir"/cur/ | wc -l)
77 if [ $num_files -ge 100 ] ; then
78 echo "Grepping through $num_files messages, this could take a while!"
80 msgfile=$(grep -HlFx "$msgid_line" "$maildir"/cur/* | head -n1)
81 if [ -z "$msgfile" ] ; then
82 die "could not find Message-ID in maildir:
83 grep -HlFx \"Message-ID: $msgid\" \"$maildir\"/cur/*"
86 # find out original message-file of linked message
87 orig_msgfile=$(readlink "$msgfile")
88 if [ -z "$orig_msgfile" ] ; then
89 die "message \"$msgfile\" is no link to another message, aborting!"
92 # get containing maildir of $orig_msgfile
93 orig_maildir=$(dirname $(dirname "$orig_msgfile"))
94 if [ ! -d "$orig_maildir/cur" ] ; then
95 die "directory \"$orig_maildir\" doesn't exist or is no Maildir"
98 # Close message-stdin and open terminal-stdin instead.
99 # mutt behaves different if STDIN is no terminal
100 # TODO: Find cleaner solution (e.g. mutt command-line argument?)
102 term="/dev/$(ps -p$$ --no-heading | awk '{print $2}')"
105 # start mutt, open original folder and jump to the original message
106 $MUTT -e "push <change-folder>$orig_maildir<enter><search>\"~i $msgid\"<enter>"