3 # notmuch-mutt - notmuch (of a) helper for Mutt
5 # Copyright: © 2011-2015 Stefano Zacchiroli <zack@upsilon.cc>
6 # License: GNU General Public License (GPL), version 3 or above
8 # See the bottom of this file for more documentation.
9 # A manpage can be obtained by running "pod2man notmuch-mutt > notmuch-mutt.1"
16 use Getopt::Long qw(:config no_getopt_compat);
18 use Mail::Box::Maildir;
20 use String::ShellQuote;
25 my $xdg_cache_dir = "$ENV{HOME}/.cache";
26 $xdg_cache_dir = $ENV{XDG_CACHE_HOME} if $ENV{XDG_CACHE_HOME};
27 my $cache_dir = "$xdg_cache_dir/notmuch/mutt";
30 # create an empty maildir (if missing) or empty an existing maildir"
31 sub empty_maildir($) {
33 rmtree($maildir) if (-d $maildir);
34 my $folder = new Mail::Box::Maildir(folder => $maildir,
39 # search($maildir, $remove_dups, $query)
40 # search mails according to $query with notmuch; store results in $maildir
42 my ($maildir, $remove_dups, $query) = @_;
45 my @args = qw/notmuch search --output=files/;
46 push @args, "--duplicate=1" if $remove_dups;
49 empty_maildir($maildir);
50 open my $pipe, '-|', @args or die "Running @args failed: $!\n";
53 my $ln = "$maildir/cur/" . basename $_;
54 symlink $_, "$ln" or warn "Failed to symlink '$_', '$ln': $!\n";
59 my ($text, $default) = @_;
61 my $term = Term::ReadLine->new( "notmuch-mutt" );
62 my $histfile = "$cache_dir/history";
64 $term->ornaments( 0 );
65 $term->unbind_key( ord( "\t" ) );
67 $histfile = $ENV{MUTT_NOTMUCH_HISTFILE} if $ENV{MUTT_NOTMUCH_HISTFILE};
68 $term->ReadHistory($histfile) if (-r $histfile);
70 chomp($query = $term->readline($text, $default));
72 system("man", "notmuch-search-terms");
74 $term->WriteHistory($histfile);
80 sub get_message_id() {
84 while (<STDIN>) { # collect header lines in @headers
88 my $head = Mail::Header->new(\@headers);
89 $mid = $head->get("message-id") or undef;
91 if ($mid) { # Message-ID header found
92 $mid =~ /^<(.*)>$/; # extract message id
94 } else { # Message-ID header not found, synthesize a message id
95 # based on SHA1, as notmuch would do. See:
96 # https://git.notmuchmail.org/git/notmuch/blob/HEAD:/lib/sha1.c
97 my $sha = Digest::SHA->new(1);
98 $sha->add($_) foreach(@headers);
99 $sha->addfile(\*STDIN);
100 $mid = 'notmuch-sha1-' . $sha->hexdigest;
106 sub search_action($$$@) {
107 my ($interactive, $results_dir, $remove_dups, @params) = @_;
109 if (! $interactive) {
110 search($results_dir, $remove_dups, join(' ', @params));
112 my $query = prompt("search ('?' for man): ", join(' ', @params));
114 search($results_dir, $remove_dups, $query);
119 sub thread_action($$@) {
120 my ($results_dir, $remove_dups, @params) = @_;
122 my $mid = get_message_id();
123 if (! defined $mid) {
124 empty_maildir($results_dir);
125 die "notmuch-mutt: cannot find Message-Id, abort.\n";
127 my $search_cmd = 'notmuch search --output=threads ' . shell_quote("id:$mid");
128 my $tid = `$search_cmd`; # get thread id
131 search($results_dir, $remove_dups, $tid);
135 my $mid = get_message_id();
136 defined $mid or die "notmuch-mutt: cannot find Message-Id, abort.\n";
138 system("notmuch", "tag", @_, "--", "id:$mid");
142 my %podflags = ( "verbose" => 1,
144 pod2usage(%podflags);
148 mkpath($cache_dir) unless (-d $cache_dir);
150 my $results_dir = "$cache_dir/results";
155 my $getopt = GetOptions(
156 "h|help" => \$help_needed,
157 "o|output-dir=s" => \$results_dir,
158 "p|prompt" => \$interactive,
159 "r|remove-dups" => \$remove_dups);
160 if (! $getopt || $#ARGV < 0) { die_usage() };
161 my ($action, @params) = ($ARGV[0], @ARGV[1..$#ARGV]);
163 foreach my $param (@params) {
164 $param =~ s/folder:=/folder:/g;
169 } elsif ($action eq "search" && $#ARGV == 0 && ! $interactive) {
170 print STDERR "Error: no search term provided\n\n";
172 } elsif ($action eq "search") {
173 search_action($interactive, $results_dir, $remove_dups, @params);
174 } elsif ($action eq "thread") {
175 thread_action($results_dir, $remove_dups, @params);
176 } elsif ($action eq "tag") {
189 notmuch-mutt - notmuch (of a) helper for Mutt
195 =item B<notmuch-mutt> [I<OPTION>]... search [I<SEARCH-TERM>]...
197 =item B<notmuch-mutt> [I<OPTION>]... thread < I<MAIL>
199 =item B<notmuch-mutt> [I<OPTION>]... tag [I<TAGS>]... < I<MAIL>
205 notmuch-mutt is a frontend to the notmuch mail indexer capable of populating
206 a maildir with search results.
214 =item --output-dir DIR
216 Store search results as (symlink) messages under maildir DIR. Beware: DIR will
217 be overwritten. (Default: F<~/.cache/notmuch/mutt/results/>)
223 Instead of using command line search terms, prompt the user for them (only for
230 Remove emails with duplicate message-ids from search results. (Passes
231 --duplicate=1 to notmuch search command.) Note this can hide search
232 results if an email accidentally or maliciously uses the same message-id
233 as a different email.
239 Show usage information and exit.
243 =head1 INTEGRATION WITH MUTT
245 notmuch-mutt can be used to integrate notmuch with the Mutt mail user agent
246 (unsurprisingly, given the name). To that end, you should define macros like
247 the following in your Mutt configuration (usually one of: F<~/.muttrc>,
248 F</etc/Muttrc>, or a configuration snippet under F</etc/Muttrc.d/>):
251 "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
252 <shell-escape>notmuch-mutt -r --prompt search<enter>\
253 <change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
254 <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
255 "notmuch: search mail"
258 "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
259 <pipe-message>notmuch-mutt -r thread<enter>\
260 <change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
261 <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
262 "notmuch: reconstruct thread"
265 "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
266 <pipe-message>notmuch-mutt tag -- -inbox<enter>\
267 <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
268 "notmuch: remove message from inbox"
270 The first macro (activated by <F8>) prompts the user for notmuch search terms
271 and then jump to a temporary maildir showing search results. The second macro
272 (activated by <F9>) reconstructs the thread corresponding to the current mail
273 and show it as search results. The third macro (activated by <F6>) removes the
274 tag C<inbox> from the current message; by changing C<-inbox> this macro may be
275 customised to add or remove tags appropriate to the users notmuch work-flow.
277 To keep notmuch index current you should then periodically run C<notmuch
278 new>. Depending on your local mail setup, you might want to do that via cron,
279 as a hook triggered by mail retrieval, etc.
287 Copyright: (C) 2011-2012 Stefano Zacchiroli <zack@upsilon.cc>
289 License: GNU General Public License (GPL), version 3 or higher