]> git.cworth.org Git - notmuch/blobdiff - contrib/notmuch-mutt/notmuch-mutt
notmuch-mutt: convert ISO-8859-15 copyright statement to UTF-8
[notmuch] / contrib / notmuch-mutt / notmuch-mutt
index 4969e4be64c3f29140e25b97ee137947058cd8ca..1e12038cdbda50bb29f6e98d49fc2b891b5cfe49 100755 (executable)
@@ -1,8 +1,8 @@
-#!/usr/bin/perl -w
+#!/usr/bin/env perl
 #
 # notmuch-mutt - notmuch (of a) helper for Mutt
 #
-# Copyright: © 2011-2012 Stefano Zacchiroli <zack@upsilon.cc> 
+# Copyright: © 2011-2015 Stefano Zacchiroli <zack@upsilon.cc>
 # License: GNU General Public License (GPL), version 3 or above
 #
 # See the bottom of this file for more documentation.
@@ -12,12 +12,14 @@ use strict;
 use warnings;
 
 use File::Path;
+use File::Basename;
 use Getopt::Long qw(:config no_getopt_compat);
-use Mail::Internet;
+use Mail::Header;
 use Mail::Box::Maildir;
 use Pod::Usage;
 use String::ShellQuote;
 use Term::ReadLine;
+use Digest::SHA;
 
 
 my $xdg_cache_dir = "$ENV{HOME}/.cache";
@@ -40,16 +42,17 @@ sub search($$$) {
     my ($maildir, $remove_dups, $query) = @_;
     my $dup_option = "";
 
-    $query = shell_quote($query);
-
-    if ($remove_dups) {
-      $dup_option = "--duplicate=1";
-    }
+    my @args = qw/notmuch search --output=files/;
+    push @args, "--duplicate=1" if $remove_dups;
+    push @args, $query;
 
     empty_maildir($maildir);
-    system("notmuch search --output=files $dup_option $query"
-          . " | sed -e 's: :\\\\ :g'"
-          . " | xargs --no-run-if-empty ln -s -t $maildir/cur/");
+    open my $pipe, '-|', @args or die "Running @args failed: $!\n";
+    while (<$pipe>) {
+       chomp;
+       my $ln = "$maildir/cur/" . basename $_;
+       symlink $_, "$ln" or warn "Failed to symlink '$_', '$ln': $!\n";
+    }
 }
 
 sub prompt($$) {
@@ -75,10 +78,29 @@ sub prompt($$) {
 }
 
 sub get_message_id() {
-    my $mail = Mail::Internet->new(\*STDIN);
-    my $mid = $mail->head->get("message-id") or return undef;
-    $mid =~ /^<(.*)>$/;        # get message-id value
-    return $1;
+    my $mid = undef;
+    my @headers = ();
+
+    while (<STDIN>) {  # collect header lines in @headers
+       push(@headers, $_);
+       last if $_ =~ /^$/;
+    }
+    my $head = Mail::Header->new(\@headers);
+    $mid = $head->get("message-id") or undef;
+
+    if ($mid) {  # Message-ID header found
+       $mid =~ /^<(.*)>$/;  # extract message id
+       $mid = $1;
+    } else {  # Message-ID header not found, synthesize a message id
+             # based on SHA1, as notmuch would do.  See:
+             # https://git.notmuchmail.org/git/notmuch/blob/HEAD:/lib/sha1.c
+       my $sha = Digest::SHA->new(1);
+       $sha->add($_) foreach(@headers);
+       $sha->addfile(\*STDIN);
+       $mid = 'notmuch-sha1-' . $sha->hexdigest;
+    }
+
+    return $mid;
 }
 
 sub search_action($$$@) {