dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] wip-filter
@ 2016-05-31 20:53 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2016-05-31 20:53 UTC (permalink / raw)
  To: spew

---
 lib/PublicInbox/Filter/Base.pm | 41 +++++++++++++++++++++++++++++++++++++++++
 lib/PublicInbox/Filter/Vger.pm |  8 ++++++++
 t/filter_base.t                | 14 ++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 lib/PublicInbox/Filter/Base.pm
 create mode 100644 lib/PublicInbox/Filter/Vger.pm
 create mode 100644 t/filter_base.t

diff --git a/lib/PublicInbox/Filter/Base.pm b/lib/PublicInbox/Filter/Base.pm
new file mode 100644
index 0000000..2bfb92d
--- /dev/null
+++ b/lib/PublicInbox/Filter/Base.pm
@@ -0,0 +1,41 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+#
+# base class for creating per-list or per-project filters
+package PublicInbox::Filter::Base;
+use strict;
+use warnings;
+sub NO_HTML () { '*** We only accept plain-text email, no HTML ***' }
+our $BAD_EXT = qr/\.(exe|bat|cmd|com|pif|scr|vbs|cpl|zip)\s*\z/i;
+
+sub REJECT () { -1 }
+sub ACCEPT { 1 }
+sub IGNORE { 0 }
+
+sub new {
+	my ($class, %opts) = @_;
+	my $self = bless { err => '', %opts }, $class;
+	# allow undef
+	$self->{bad_ext} = $BAD_EXT unless exists $self->{bad_ext};
+	$self;
+}
+
+sub reject ($$) {
+	my ($self, $reason) = @_;
+	$self->{err} = $reason;
+	REJECT;
+}
+
+sub err ($) { $_[0]->{err} }
+
+sub mda_filter ($$) {
+	my ($self, $mime) = @_;
+
+	# scan through all parts once
+	$mime->walk_parts(sub {
+		my ($part) = @_;
+		return if $part->subparts; # walk_parts already recurses
+	});
+}
+
+1;
diff --git a/lib/PublicInbox/Filter/Vger.pm b/lib/PublicInbox/Filter/Vger.pm
new file mode 100644
index 0000000..b7db071
--- /dev/null
+++ b/lib/PublicInbox/Filter/Vger.pm
@@ -0,0 +1,8 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+package PublicInbox::Filter::Vger;
+use base qw(PublicInbox::Filter::Base);
+use strict;
+use warnings;
+
+1;
diff --git a/t/filter_base.t b/t/filter_base.t
new file mode 100644
index 0000000..7854faa
--- /dev/null
+++ b/t/filter_base.t
@@ -0,0 +1,14 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use Email::MIME;
+use_ok 'PublicInbox::Filter::Base';
+
+{
+	my $f = PublicInbox::Filter::Base->new;
+	ok($f, 'created stock object');
+}
+
+done_testing();
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] wip-filter
@ 2016-06-10  1:00 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2016-06-10  1:00 UTC (permalink / raw)
  To: spew

---
 lib/PublicInbox/Filter/Base.pm | 91 ++++++++++++++++++++++++++++++++++++++++++
 lib/PublicInbox/Filter/Vger.pm |  8 ++++
 t/filter_base.t                | 22 ++++++++++
 3 files changed, 121 insertions(+)
 create mode 100644 lib/PublicInbox/Filter/Base.pm
 create mode 100644 lib/PublicInbox/Filter/Vger.pm
 create mode 100644 t/filter_base.t

diff --git a/lib/PublicInbox/Filter/Base.pm b/lib/PublicInbox/Filter/Base.pm
new file mode 100644
index 0000000..a969e6e
--- /dev/null
+++ b/lib/PublicInbox/Filter/Base.pm
@@ -0,0 +1,91 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+#
+# base class for creating per-list or per-project filters
+package PublicInbox::Filter::Base;
+use strict;
+use warnings;
+use PublicInbox::MsgIter;
+
+my $NO_HTML = '*** We only accept plain-text mail, no HTML ***';
+our %DEFAULTS = (
+	reject_suffix => [ qw(exe bat cmd com pif scr vbs cpl zip) ],
+	reject_type => [ "text/html:$NO_HTML", "text/xhtml:$NO_HTML" ],
+);
+our $INVALID_FN = qr/\0/;
+
+sub REJECT () { -1 }
+sub ACCEPT () { 1 }
+sub IGNORE () { 0 }
+
+sub new {
+	my ($class, %opts) = @_;
+	my $self = bless { err => '', %opts }, $class;
+	foreach my $f (qw(reject_suffix reject_type)) {
+		# allow undef:
+		$self->{$f} = $DEFAULTS{$f} unless exists $self->{$f};
+	}
+	if (defined $self->{reject_suffix}) {
+		my $tmp = $self->{reject_suffix};
+		$tmp = join('|', map { quotemeta } @$tmp);
+		$self->{reject_suffix} = qr/\.($tmp)\s*\z/i;
+	}
+	my $rt_msg = $self->{-reject_type_msg} = {};
+	if (defined $self->{reject_type}) {
+		my $tmp = $self->{reject_type};
+		@$tmp = map {
+			my ($type, $msg) = split(':', $_, 2);
+			$type = lc $type;
+			$msg ||= "Unacceptable Content-Type: $type";
+			$rt_msg->{$type} = $msg;
+			quotemeta $type;
+		} @$tmp;
+		$tmp = join('|', @$tmp);
+		$self->{reject_type} = qr/\b($tmp)\b/i;
+	}
+	$self;
+}
+
+sub reject ($$) {
+	my ($self, $reason) = @_;
+	$self->{err} = $reason;
+	REJECT;
+}
+
+sub err ($) { $_[0]->{err} }
+
+# for MDA
+sub delivery ($$) {
+	my ($self, $mime) = @_;
+
+	my $reject_type = $self->{reject_type};
+	my $reject_suffix = $self->{reject_suffix} || $INVALID_FN;
+	my $rt_msg = $self->{-reject_type_msg};
+	my (%sfx, %type);
+
+	msg_iter($mime, sub {
+		my ($part, $depth, @idx) = @{$_[0]};
+
+		my $ct = $part->content_type || 'text/plain';
+		if ($ct =~ $reject_type) {
+			$type{$rt_msg->{lc $1}} = 1;
+		}
+
+		my $fn = $part->filename;
+		if (defined($fn) && $fn =~ $reject_suffix) {
+			$sfx{$1} = 1;
+		}
+	});
+
+	my @r;
+	if (keys %type) {
+		push @r, sort keys %type;
+	}
+	if (keys %sfx) {
+		push @r, 'Rejected suffixes(s): '.join(', ', sort keys %sfx);
+	}
+
+	@r ? $self->reject(join("\n", @r)) : $self->ACCEPT;
+}
+
+1;
diff --git a/lib/PublicInbox/Filter/Vger.pm b/lib/PublicInbox/Filter/Vger.pm
new file mode 100644
index 0000000..b7db071
--- /dev/null
+++ b/lib/PublicInbox/Filter/Vger.pm
@@ -0,0 +1,8 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+package PublicInbox::Filter::Vger;
+use base qw(PublicInbox::Filter::Base);
+use strict;
+use warnings;
+
+1;
diff --git a/t/filter_base.t b/t/filter_base.t
new file mode 100644
index 0000000..47c17ec
--- /dev/null
+++ b/t/filter_base.t
@@ -0,0 +1,22 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use Email::MIME;
+use_ok 'PublicInbox::Filter::Base';
+
+{
+	my $f = PublicInbox::Filter::Base->new;
+	ok($f, 'created stock object');
+	ok(defined $f->{reject_suffix}, 'rejected suffix redefined');
+	is(ref($f->{reject_suffix}), 'Regexp', 'reject_suffix should be a RE');
+}
+
+{
+	my $f = PublicInbox::Filter::Base->new(reject_suffix => undef);
+	ok($f, 'created base object q/o reject_suffix');
+	ok(!defined $f->{reject_suffix}, 'reject_suffix not defined');
+}
+
+done_testing();

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-06-10  1:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-31 20:53 [PATCH] wip-filter Eric Wong
  -- strict thread matches above, loose matches on Subject: below --
2016-06-10  1:00 Eric Wong

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).