unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Tomi Ollila <tomi.ollila@iki.fi>
To: Stefano Zacchiroli <zack@upsilon.cc>, notmuch@notmuchmail.org
Subject: Re: [PATCH 2/2] notmuch-mutt: support for messages that lack Message-ID headers
Date: Mon, 09 Feb 2015 16:31:58 +0200	[thread overview]
Message-ID: <m2twyvyx41.fsf@guru.guru-group.fi> (raw)
In-Reply-To: <1423474890-16972-3-git-send-email-zack@upsilon.cc>

On Mon, Feb 09 2015, Stefano Zacchiroli <zack@upsilon.cc> wrote:

> From: "Jan N. Klug" <jan.n.klug@rub.de>
>
> For those messages, compute a synthetic Message-ID based on the SHA1
> of the whole message, in the same way that notmuch would do. See:
> http://git.notmuchmail.org/git/notmuch/blob/HEAD:/lib/sha1.c
>
> To do the above, rewrite get_message_id() to scan the current message
> line by line, incrementally computing a SHA1. If a Message-ID is found
> the SHA1 computation will be aborted; otherwise used for the synthetic
> Message-ID.

I'd suggest the (something like) following way:

First loop through headers, push lines to the array.

check for Message-Id (using Mail::Header): if found, return it

else message id not found:
 
  my $sha = Digest::SHA->new(1);
  $sha->add($_) foreach(@raw_header);
  $sha->addfile(\*STDIN);
  return  'notmuch-sha1-' . $sha->hexdigest;

(BTW: the implementation in the patch (below) would check for message-id in
@raw_header every time mail has an empty line ?)

Tomi


>
> Signed-off-by: Stefano Zacchiroli <zack@upsilon.cc>
> ---
>  contrib/notmuch-mutt/README       |  4 +++-
>  contrib/notmuch-mutt/notmuch-mutt | 33 ++++++++++++++++++++++++++++-----
>  2 files changed, 31 insertions(+), 6 deletions(-)
>
> diff --git a/contrib/notmuch-mutt/README b/contrib/notmuch-mutt/README
> index c661447..9c3379e 100644
> --- a/contrib/notmuch-mutt/README
> +++ b/contrib/notmuch-mutt/README
> @@ -33,9 +33,11 @@ Requirements
>  
>  To *run* notmuch-mutt you will need Perl with the following libraries:
>  
> +- Digest::SHA <https://metacpan.org/release/Digest-SHA>
> +  (Debian package: libdigest-sha-perl)
>  - Mail::Box <https://metacpan.org/pod/Mail::Box>
>    (Debian package: libmail-box-perl)
> -- Mail::Internet <https://metacpan.org/pod/Mail::Internet>
> +- Mail::Header <https://metacpan.org/pod/Mail::Header>
>    (Debian package: libmailtools-perl)
>  - String::ShellQuote <https://metacpan.org/pod/String::ShellQuote>
>    (Debian package: libstring-shellquote-perl)
> diff --git a/contrib/notmuch-mutt/notmuch-mutt b/contrib/notmuch-mutt/notmuch-mutt
> index 4969e4b..84af140 100755
> --- a/contrib/notmuch-mutt/notmuch-mutt
> +++ b/contrib/notmuch-mutt/notmuch-mutt
> @@ -13,11 +13,12 @@ use warnings;
>  
>  use File::Path;
>  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";
> @@ -75,10 +76,32 @@ 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 $in_header = 1;
> +    my @raw_header = ();
> +    my $sha = Digest::SHA->new(1);  # SHA1 hash of the whole mail
> +
> +    while (<STDIN>) {  # compute SHA1 as we go
> +	push(@raw_header, $_) if $in_header;  # cache header lines
> +        if ($_ =~ /^$/) {  # end of header, parse it and look for Message-ID
> +	    $in_header = 0;
> +	    my $head = Mail::Header->new(\@raw_header);
> +	    $mid = $head->get("message-id") or undef;
> +	    if ($mid) {
> +		$mid =~ /^<(.*)>$/;  # get message-id value
> +		$mid = $1;
> +		last;  # stop hashing
> +	    }
> +        }
> +	$sha->add($_);  # update hash
> +    }
> +
> +    # If no message-id was found, generate one-id in the same way that
> +    # notmuch would do.
> +    # See: http://git.notmuchmail.org/git/notmuch/blob/HEAD:/lib/sha1.c
> +    $mid ||= "notmuch-sha1-".$sha->hexdigest;
> +
> +    return $mid;
>  }
>  
>  sub search_action($$$@) {
> -- 
> 2.1.4
>
> _______________________________________________
> notmuch mailing list
> notmuch@notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch

  reply	other threads:[~2015-02-09 14:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-09  9:41 notmuch-mutt: support for messages that lack Message-ID (v2) Stefano Zacchiroli
2015-02-09  9:41 ` [PATCH 1/2] notmuch-mutt README: use metacpn.org/* as deps homepages Stefano Zacchiroli
2015-02-09  9:41 ` [PATCH 2/2] notmuch-mutt: support for messages that lack Message-ID headers Stefano Zacchiroli
2015-02-09 14:31   ` Tomi Ollila [this message]
2015-02-15 12:39     ` notmuch-mutt: support for messages that lack Message-ID headers (v3) Stefano Zacchiroli
2015-02-15 12:39       ` [PATCH 1/3] notmuch-mutt: bump copyright year Stefano Zacchiroli
2015-02-15 12:39       ` [PATCH 2/3] notmuch-mutt README: use metacpn.org/* as deps homepages Stefano Zacchiroli
2015-02-15 12:39       ` [PATCH 3/3] notmuch-mutt: support for messages that lack Message-ID headers Stefano Zacchiroli
2015-02-15 16:46       ` notmuch-mutt: support for messages that lack Message-ID headers (v3) Tomi Ollila
2015-02-16 13:00       ` David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2twyvyx41.fsf@guru.guru-group.fi \
    --to=tomi.ollila@iki.fi \
    --cc=notmuch@notmuchmail.org \
    --cc=zack@upsilon.cc \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).