]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Filter/RubyLang.pm
filter/rubylang: reuse altid entry from inbox object
[public-inbox.git] / lib / PublicInbox / Filter / RubyLang.pm
1 # Copyright (C) 2017 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Filter for lists.ruby-lang.org trailers
5 package PublicInbox::Filter::RubyLang;
6 use base qw(PublicInbox::Filter::Base);
7 use strict;
8 use warnings;
9
10 my $l1 = qr/Unsubscribe:\s
11         <mailto:ruby-\w+-request\@ruby-lang\.org\?subject=unsubscribe>/x;
12 my $l2 = qr{<http://lists\.ruby-lang\.org/cgi-bin/mailman/options/ruby-\w+>};
13
14 sub new {
15         my ($class, %opts) = @_;
16         my $altid = delete $opts{-altid};
17         my $self = $class->SUPER::new(%opts);
18         my $ibx = $self->{-inbox};
19         # altid = serial:ruby-core:file=msgmap.sqlite3
20         if (!$altid && $ibx && $ibx->{altid}) {
21                 $altid ||= $ibx->{altid}->[0];
22         }
23         if ($altid) {
24                 require PublicInbox::MID; # mid_clean
25                 require PublicInbox::AltId;
26                 $self->{-altid} = PublicInbox::AltId->new($ibx, $altid, 1);
27         }
28         $self;
29 }
30
31 sub scrub {
32         my ($self, $mime) = @_;
33         # no msg_iter here, that is only for read-only access
34         $mime->walk_parts(sub {
35                 my ($part) = $_[0];
36                 my $ct = $part->content_type;
37                 if (!$ct || $ct =~ m{\btext/plain\b}i) {
38                         my $s = eval { $part->body_str };
39                         if (defined $s && $s =~ s/\n?$l1\n$l2\n\z//os) {
40                                 $part->body_str_set($s);
41                         }
42                 }
43         });
44         my $altid = $self->{-altid};
45         if ($altid) {
46                 my $hdr = $mime->header_obj;
47                 my $mid = $hdr->header_raw('Message-ID');
48                 unless (defined $mid) {
49                         return $self->REJECT('Message-Id missing');
50                 }
51                 my $n = $hdr->header_raw('X-Mail-Count');
52                 if (!defined($n) || $n !~ /\A\s*\d+\s*\z/) {
53                         return $self->REJECT('X-Mail-Count not numeric');
54                 }
55                 $mid = PublicInbox::MID::mid_clean($mid);
56                 $altid->{mm_alt}->mid_set($n, $mid);
57         }
58         $self->ACCEPT($mime);
59 }
60
61 sub delivery {
62         my ($self, $mime) = @_;
63         $self->scrub($mime);
64 }
65
66 1;