]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Filter/RubyLang.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / Filter / RubyLang.pm
1 # Copyright (C) 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 v5.10.1;
7 use parent qw(PublicInbox::Filter::Base);
8 use PublicInbox::MID qw(mids);
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->{ibx};
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::AltId;
25                 $self->{-altid} = PublicInbox::AltId->new($ibx, $altid, 1);
26         }
27         $self;
28 }
29
30 sub scrub_part ($) {
31         my ($part) = @_;
32         my $ct = $part->content_type;
33         if (!$ct || $ct =~ m{\btext/plain\b}i) {
34                 my $s = eval { $part->body_str };
35                 if (defined $s && $s =~ s/\n?$l1\n$l2\n\z//os) {
36                         $part->body_str_set($s);
37                         return 1;
38                 }
39         }
40         0;
41 }
42
43 sub scrub {
44         my ($self, $mime, $for_remove) = @_;
45         # no msg_iter here, msg_iter is only for read-only access
46         if (my @sub = $mime->subparts) {
47                 my $changed = 0;
48                 $changed |= scrub_part($_) for @sub;
49                 $mime->parts_set(\@sub) if $changed;
50         } else {
51                 scrub_part($mime);
52         }
53         my $altid = $self->{-altid};
54         if ($altid && !$for_remove) {
55                 my $hdr = $mime->header_obj;
56                 my $mids = mids($hdr);
57                 return $self->REJECT('Message-ID missing') unless (@$mids);
58                 my $n;
59                 my @v = $hdr->header_raw('X-Mail-Count'); # old host only
60                 if (@v) {
61                         for (@v) {
62                                 /\A\s*([0-9]+)\s*\z/ or next;
63                                 $n = $1;
64                                 last;
65                         }
66                 } else { # new host: nue.mailmanlists.eu
67                         for ($hdr->header_str('Subject')) {
68                                 /\A\[ruby-[^:]+:([0-9]+)\]/ or next;
69                                 $n = $1;
70                                 last;
71                         }
72                 }
73                 $n // return $self->REJECT('could not get count not numeric');
74                 foreach my $mid (@$mids) {
75                         my $r = $altid->mm_alt->mid_set($n, $mid);
76                         next if $r == 0;
77                         last;
78                 }
79         }
80         $self->ACCEPT($mime);
81 }
82
83 sub delivery {
84         my ($self, $mime) = @_;
85         $self->scrub($mime);
86 }
87
88 1;