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"
17 use Getopt::Long qw(:config no_getopt_compat);
19 use Mail::Box::Maildir;
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 my ($maildir, $error) = @_;
31 die "notmuch-mutt: search cache maildir $maildir $error\n".
32 "Please ensure that the notmuch-mutt search cache Maildir\n".
33 "contains no subfolders or real mail data, only symlinks to mail\n";
37 my ($maildir, $subdir, $error) = @_;
38 die_dir($maildir, "subdir $subdir $error");
41 # check that the search cache maildir is that and not a real maildir
42 # otherwise there could be data loss when the search cache is emptied
43 sub check_search_cache_maildir($) {
46 return unless -e $maildir;
48 -d $maildir or die_dir($maildir, 'is not a directory');
50 opendir(my $mdh, $maildir) or die_dir($maildir, "cannot be opened: $!");
51 my @contents = grep { !/^\.\.?$/ } readdir $mdh;
54 my @required = ('cur', 'new', 'tmp');
55 foreach my $d (@required) {
56 -l "$maildir/$d" and die_dir($maildir, "contains symlink $d");
57 -e "$maildir/$d" or die_subdir($maildir, $d, 'is missing');
58 -d "$maildir/$d" or die_subdir($maildir, $d, 'is not a directory');
61 $_ eq '..' and return;
62 -l $_ or die_subdir($maildir, $d, "contains non-symlink $_");
66 my %required = map { $_ => 1 } @required;
67 foreach my $d (@contents) {
68 -l "$maildir/$d" and die_dir( $maildir, "contains symlink $d");
69 -d "$maildir/$d" or die_dir( $maildir, "contains non-directory $d");
70 exists($required{$d}) or die_dir( $maildir, "contains directory $d");
74 # create an empty search cache maildir (if missing) or empty existing one
75 sub empty_search_cache_maildir($) {
77 rmtree($maildir) if (-d $maildir);
78 my $folder = new Mail::Box::Maildir(folder => $maildir,
83 # search($maildir, $remove_dups, $query)
84 # search mails according to $query with notmuch; store results in $maildir
86 my ($maildir, $remove_dups, $query) = @_;
89 my @args = qw/notmuch search --output=files/;
90 push @args, "--duplicate=1" if $remove_dups;
93 check_search_cache_maildir($maildir);
94 empty_search_cache_maildir($maildir);
95 open my $pipe, '-|', @args or die "Running @args failed: $!\n";
98 my $ln = "$maildir/cur/" . basename $_;
99 symlink $_, "$ln" or warn "Failed to symlink '$_', '$ln': $!\n";
104 my ($text, $default) = @_;
106 my $term = Term::ReadLine->new( "notmuch-mutt" );
107 my $histfile = "$cache_dir/history";
109 $term->ornaments( 0 );
110 $term->unbind_key( ord( "\t" ) );
112 $histfile = $ENV{MUTT_NOTMUCH_HISTFILE} if $ENV{MUTT_NOTMUCH_HISTFILE};
113 $term->ReadHistory($histfile) if (-r $histfile);
115 chomp($query = $term->readline($text, $default));
117 system("man", "notmuch-search-terms");
119 $term->WriteHistory($histfile);
125 sub get_message_id() {
129 while (<STDIN>) { # collect header lines in @headers
133 my $head = Mail::Header->new(\@headers);
134 $mid = $head->get("message-id") or undef;
136 if ($mid) { # Message-ID header found
137 $mid =~ /^<(.*)>$/; # extract message id
139 } else { # Message-ID header not found, synthesize a message id
140 # based on SHA1, as notmuch would do. See:
141 # https://git.notmuchmail.org/git/notmuch/blob/HEAD:/lib/sha1.c
142 my $sha = Digest::SHA->new(1);
143 $sha->add($_) foreach(@headers);
144 $sha->addfile(\*STDIN);
145 $mid = 'notmuch-sha1-' . $sha->hexdigest;
151 sub search_action($$$@) {
152 my ($interactive, $results_dir, $remove_dups, @params) = @_;
154 if (! $interactive) {
155 search($results_dir, $remove_dups, join(' ', @params));
157 my $query = prompt("search ('?' for man): ", join(' ', @params));
159 search($results_dir, $remove_dups, $query);
164 sub thread_action($$@) {
165 my ($results_dir, $remove_dups, @params) = @_;
167 my $mid = get_message_id();
168 if (! defined $mid) {
169 die "notmuch-mutt: cannot find Message-Id, abort.\n";
172 $mid =~ s/ //g; # notmuch strips spaces before storing Message-Id
173 $mid =~ s/"/""""/g; # escape all double quote characters twice
175 search($results_dir, $remove_dups, qq{thread:"{id:""$mid""}"});
179 my $mid = get_message_id();
180 defined $mid or die "notmuch-mutt: cannot find Message-Id, abort.\n";
182 $mid =~ s/ //g; # notmuch strips spaces before storing Message-Id
183 $mid =~ s/"/""/g; # escape all double quote characters
185 system("notmuch", "tag", @_, "--", qq{id:"$mid"});
189 my %podflags = ( "verbose" => 1,
191 pod2usage(%podflags);
195 mkpath($cache_dir) unless (-d $cache_dir);
197 my $results_dir = "$cache_dir/results";
202 my $getopt = GetOptions(
203 "h|help" => \$help_needed,
204 "o|output-dir=s" => \$results_dir,
205 "p|prompt" => \$interactive,
206 "r|remove-dups" => \$remove_dups);
207 if (! $getopt || $#ARGV < 0) { die_usage() };
208 my ($action, @params) = ($ARGV[0], @ARGV[1..$#ARGV]);
210 foreach my $param (@params) {
211 $param =~ s/folder:=/folder:/g;
216 } elsif ($action eq "search" && $#ARGV == 0 && ! $interactive) {
217 print STDERR "Error: no search term provided\n\n";
219 } elsif ($action eq "search") {
220 search_action($interactive, $results_dir, $remove_dups, @params);
221 } elsif ($action eq "thread") {
222 thread_action($results_dir, $remove_dups, @params);
223 } elsif ($action eq "tag") {
236 notmuch-mutt - notmuch (of a) helper for Mutt
242 =item B<notmuch-mutt> [I<OPTION>]... search [I<SEARCH-TERM>]...
244 =item B<notmuch-mutt> [I<OPTION>]... thread < I<MAIL>
246 =item B<notmuch-mutt> [I<OPTION>]... tag [I<TAGS>]... < I<MAIL>
252 notmuch-mutt is a frontend to the notmuch mail indexer capable of populating
253 a maildir with search results.
261 =item --output-dir DIR
263 Store search results as (symlink) messages under maildir DIR. Beware: DIR will
264 be overwritten. (Default: F<~/.cache/notmuch/mutt/results/>)
270 Instead of using command line search terms, prompt the user for them (only for
277 Remove emails with duplicate message-ids from search results. (Passes
278 --duplicate=1 to notmuch search command.) Note this can hide search
279 results if an email accidentally or maliciously uses the same message-id
280 as a different email.
286 Show usage information and exit.
290 =head1 INTEGRATION WITH MUTT
292 notmuch-mutt can be used to integrate notmuch with the Mutt mail user agent
293 (unsurprisingly, given the name). To that end, you should define macros like
294 the following in your Mutt configuration (usually one of: F<~/.muttrc>,
295 F</etc/Muttrc>, or a configuration snippet under F</etc/Muttrc.d/>):
298 "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
299 <shell-escape>notmuch-mutt -r --prompt search<enter>\
300 <change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
301 <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
302 "notmuch: search mail"
305 "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
306 <pipe-message>notmuch-mutt -r thread<enter>\
307 <change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
308 <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
309 "notmuch: reconstruct thread"
312 "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
313 <pipe-message>notmuch-mutt tag -- -inbox<enter>\
314 <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
315 "notmuch: remove message from inbox"
317 The first macro (activated by <F8>) prompts the user for notmuch search terms
318 and then jump to a temporary maildir showing search results. The second macro
319 (activated by <F9>) reconstructs the thread corresponding to the current mail
320 and show it as search results. The third macro (activated by <F6>) removes the
321 tag C<inbox> from the current message; by changing C<-inbox> this macro may be
322 customised to add or remove tags appropriate to the users notmuch work-flow.
324 To keep notmuch index current you should then periodically run C<notmuch
325 new>. Depending on your local mail setup, you might want to do that via cron,
326 as a hook triggered by mail retrieval, etc.
334 Copyright: (C) 2011-2012 Stefano Zacchiroli <zack@upsilon.cc>
336 License: GNU General Public License (GPL), version 3 or higher