From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Cc: Eric Wong <e@80x24.org>
Subject: [PATCH] extract redundant Message-ID handling code
Date: Fri, 14 Aug 2015 19:22:29 +0000 [thread overview]
Message-ID: <1439580149-11355-1-git-send-email-e@80x24.org> (raw)
Quit repeating ourselves and use a common MID module
instead.
---
lib/PublicInbox/Hval.pm | 13 +++----------
lib/PublicInbox/MID.pm | 24 ++++++++++++++++++++++++
lib/PublicInbox/Search.pm | 31 ++++++-------------------------
lib/PublicInbox/View.pm | 8 +++-----
4 files changed, 36 insertions(+), 40 deletions(-)
create mode 100644 lib/PublicInbox/MID.pm
diff --git a/lib/PublicInbox/Hval.pm b/lib/PublicInbox/Hval.pm
index 68f8954..d8b31c8 100644
--- a/lib/PublicInbox/Hval.pm
+++ b/lib/PublicInbox/Hval.pm
@@ -8,6 +8,7 @@ use warnings;
use fields qw(raw href);
use Encode qw(find_encoding);
use URI::Escape qw(uri_escape_utf8);
+use PublicInbox::MID qw/mid_clean mid_compressed/;
my $enc_ascii = find_encoding('us-ascii');
@@ -25,16 +26,8 @@ sub new {
sub new_msgid {
my ($class, $msgid) = @_;
- $msgid =~ s/\A\s*<?//;
- $msgid =~ s/>?\s*\z//;
-
- if (length($msgid) <= 40) {
- $class->new($msgid);
- } else {
- require Digest::SHA;
- my $hex = Digest::SHA::sha1_hex($msgid);
- $class->new($msgid, $hex);
- }
+ $msgid = mid_clean($msgid);
+ $class->new($msgid, mid_compressed($msgid));
}
sub new_oneline {
diff --git a/lib/PublicInbox/MID.pm b/lib/PublicInbox/MID.pm
new file mode 100644
index 0000000..b56ce03
--- /dev/null
+++ b/lib/PublicInbox/MID.pm
@@ -0,0 +1,24 @@
+# Copyright (C) 2015, all contributors <meta@public-inbox.org>
+# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+use base qw/Exporter/;
+our @EXPORT = qw/mid_clean mid_compressed/;
+use Digest::SHA qw/sha1_hex/;
+use constant MID_MAX => 40; # SHA-1 hex length
+
+sub mid_clean {
+ my ($mid) = @_;
+ defined($mid) or die "no Message-ID";
+ # MDA->precheck did more checking for us
+ $mid =~ s/\A\s*<?//;
+ $mid =~ s/>?\s*\z//;
+ $mid;
+}
+
+# this is idempotent
+sub mid_compressed {
+ my ($mid) = @_;
+ return $mid if (length($mid) <= MID_MAX);
+ sha1_hex($mid);
+}
+
+1;
diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index fe4984e..328c9a2 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -6,25 +6,22 @@ use strict;
use warnings;
use PublicInbox::SearchMsg;
use base qw/Exporter/;
-use Digest::SHA qw//;
use Search::Xapian qw/:standard/;
require PublicInbox::View;
use Date::Parse qw/str2time/;
use POSIX qw//;
use Email::MIME;
+use PublicInbox::MID qw/mid_clean mid_compressed/;
-our @EXPORT = qw/xpfx mid_compressed/;
+our @EXPORT = qw/xpfx/;
use constant {
TS => 0,
- SHA1HEX_LEN => 40,
SCHEMA_VERSION => 0,
LANG => 'english',
QP_FLAGS => FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE|FLAG_WILDCARD,
};
-use constant MID_MAX => SHA1HEX_LEN;
-
# setup prefixes
my %bool_pfx_internal = (
type => 'T', # "mail" or "ghost"
@@ -54,13 +51,6 @@ while (my ($k, $v) = each %all_pfx) {
my $mail_query = Search::Xapian::Query->new(xpfx('type') . 'mail');
-# this is idempotent
-sub mid_compressed {
- my ($mid) = @_;
- return $mid if (length($mid) <= MID_MAX);
- Digest::SHA::sha1_hex($mid);
-}
-
sub new {
my ($class, $git_dir, $writable) = @_;
# allow concurrent versions for easier rollback:
@@ -86,7 +76,7 @@ sub add_message {
my $db = $self->{xdb};
my $doc_id;
- my $mid = clean_mid($mime->header('Message-ID'));
+ my $mid = mid_clean($mime->header('Message-ID'));
$mid = mid_compressed($mid);
my $was_ghost = 0;
my $ct_msg = $mime->header('Content-Type') || 'text/plain';
@@ -211,7 +201,7 @@ sub remove_message {
my ($self, $mid) = @_;
my $db = $self->{xdb};
my $doc_id;
- $mid = clean_mid($mid);
+ $mid = mid_clean($mid);
$mid = mid_compressed($mid);
$db->begin_transaction;
@@ -242,7 +232,7 @@ sub query {
# given a message ID, get replies to a message
sub get_replies {
my ($self, $mid, $opts) = @_;
- $mid = clean_mid($mid);
+ $mid = mid_clean($mid);
$mid = mid_compressed($mid);
my $qp = $self->qp;
my $irt = $qp->parse_query("inreplyto:$mid", 0);
@@ -345,15 +335,6 @@ sub date_range_processor {
$_[0]->{drp} ||= Search::Xapian::DateValueRangeProcessor->new(TS);
}
-sub clean_mid {
- my ($mid) = @_;
- defined($mid) or die "no Message-ID";
- # MDA->precheck did more checking for us
- $mid =~ s/\A\s*<?//;
- $mid =~ s/>?\s*\z//;
- $mid;
-}
-
sub link_message {
my ($self, $smsg, $is_ghost) = @_;
@@ -411,7 +392,7 @@ sub link_message_to_parents {
sub lookup_message {
my ($self, $mid) = @_;
- $mid = clean_mid($mid);
+ $mid = mid_clean($mid);
$mid = mid_compressed($mid);
my $doc_id = $self->find_unique_doc_id('mid', $mid);
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 30759a3..c2dbb7e 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -3,11 +3,12 @@
package PublicInbox::View;
use strict;
use warnings;
-use PublicInbox::Hval;
use URI::Escape qw/uri_escape_utf8/;
use Encode qw/find_encoding/;
use Encode::MIME::Header;
use Email::MIME::ContentType qw/parse_content_type/;
+use PublicInbox::Hval;
+use PublicInbox::MID qw/mid_clean mid_compressed/;
require POSIX;
# TODO: make these constants tunable
@@ -366,12 +367,9 @@ sub linkify_refs {
} @_);
}
-require Digest::SHA;
sub anchor_for {
my ($msgid) = @_;
- $msgid =~ s/\A\s*<?//;
- $msgid =~ s/>?\s*\z//;
- 'm' . Digest::SHA::sha1_hex($msgid);
+ 'm' . mid_compressed(mid_clean($msgid));
}
1;
--
EW
next reply other threads:[~2015-08-14 19:23 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-14 19:22 Eric Wong [this message]
2015-08-15 8:15 ` [PATCH v2] extract redundant Message-ID handling code Eric Wong
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://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1439580149-11355-1-git-send-email-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/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.
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).