[pmg-devel] [PATCH pmg-api 2/2] add new What Object 'Match Archive Filename'

Dominik Csapak d.csapak at proxmox.com
Tue Apr 14 14:54:40 CEST 2020


This behaves like the 'ArchiveFilter' to 'ContentTypeFilter',
in that it matches the filenames in archives, as well as outside.

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/Makefile                           |   1 +
 src/PMG/API2/What.pm                   |   2 +
 src/PMG/RuleCache.pm                   |   4 +-
 src/PMG/RuleDB.pm                      |   4 +
 src/PMG/RuleDB/MatchArchiveFilename.pm | 115 +++++++++++++++++++++++++
 5 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100644 src/PMG/RuleDB/MatchArchiveFilename.pm

diff --git a/src/Makefile b/src/Makefile
index b5e4c9f..05d9598 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -87,6 +87,7 @@ LIBSOURCES =				\
 	PMG/RuleDB/IPNet.pm		\
 	PMG/RuleDB/ModField.pm		\
 	PMG/RuleDB/MatchFilename.pm	\
+	PMG/RuleDB/MatchArchiveFilename.pm	\
 	PMG/RuleDB/ReceiverRegex.pm	\
 	PMG/RuleDB/EMail.pm		\
 	PMG/RuleDB/Receiver.pm		\
diff --git a/src/PMG/API2/What.pm b/src/PMG/API2/What.pm
index 14b9053..bba3003 100644
--- a/src/PMG/API2/What.pm
+++ b/src/PMG/API2/What.pm
@@ -55,6 +55,7 @@ __PACKAGE__->register_method ({
 	    { subdir => 'archivefilter' },
 	    { subdir => 'filenamefilter' },
 	    { subdir => 'virusfilter' },
+	    { subdir => 'archivefilenamefilter' },
 	];
 
     }});
@@ -69,5 +70,6 @@ PMG::RuleDB::Spam->register_api(__PACKAGE__, 'spamfilter');
 PMG::RuleDB::ArchiveFilter->register_api(__PACKAGE__, 'archivefilter');
 PMG::RuleDB::MatchFilename->register_api(__PACKAGE__, 'filenamefilter');
 PMG::RuleDB::Virus->register_api(__PACKAGE__, 'virusfilter');
+PMG::RuleDB::MatchArchiveFilename->register_api(__PACKAGE__, 'archivefilenamefilter');
 
 1;
diff --git a/src/PMG/RuleCache.pm b/src/PMG/RuleCache.pm
index 9b18e7e..655f4a2 100644
--- a/src/PMG/RuleCache.pm
+++ b/src/PMG/RuleCache.pm
@@ -98,7 +98,9 @@ sub new {
 			push @$when,  $obj;
 		    } elsif ($gtype == 3) { # what
 			push @$what,  $obj;
-			if ($obj->otype == PMG::RuleDB::ArchiveFilter->otype) {
+			if ($obj->otype == PMG::RuleDB::ArchiveFilter->otype ||
+			    $obj->otype == PMG::RuleDB::MatchArchiveFilename->otype)
+			{
 			    if ($rule->{direction} == 0) {
 				$self->{archivefilter_in} = 1;
 			    } elsif ($rule->{direction} == 1) {
diff --git a/src/PMG/RuleDB.pm b/src/PMG/RuleDB.pm
index 9b8abd0..8b38ac0 100644
--- a/src/PMG/RuleDB.pm
+++ b/src/PMG/RuleDB.pm
@@ -34,6 +34,7 @@ use PMG::RuleDB::Remove;
 use PMG::RuleDB::ModField;
 use PMG::RuleDB::MatchField;
 use PMG::RuleDB::MatchFilename;
+use PMG::RuleDB::MatchArchiveFilename;
 use PMG::RuleDB::Attach;
 use PMG::RuleDB::Disclaimer;
 use PMG::RuleDB::BCC;
@@ -338,6 +339,9 @@ sub get_object {
     elsif ($otype == PMG::RuleDB::MatchFilename::otype) {
         $obj = PMG::RuleDB::MatchFilename->new();
     }
+    elsif ($otype == PMG::RuleDB::MatchArchiveFilename::otype) {
+        $obj = PMG::RuleDB::MatchArchiveFilename->new();
+    }
     elsif ($otype == PMG::RuleDB::ContentTypeFilter::otype) {
         $obj = PMG::RuleDB::ContentTypeFilter->new();
     }
diff --git a/src/PMG/RuleDB/MatchArchiveFilename.pm b/src/PMG/RuleDB/MatchArchiveFilename.pm
new file mode 100644
index 0000000..defc51e
--- /dev/null
+++ b/src/PMG/RuleDB/MatchArchiveFilename.pm
@@ -0,0 +1,115 @@
+package PMG::RuleDB::MatchArchiveFilename;
+
+use strict;
+use warnings;
+
+use PMG::Utils;
+use PMG::RuleDB::MatchFilename;
+
+use base qw(PMG::RuleDB::MatchFilename);
+
+sub otype {
+    return 3006;
+}
+
+sub oclass {
+    return 'what';
+}
+
+sub otype_text {
+    return 'Match Archive Filename';
+}
+
+sub new {
+    my ($type, $fname, $ogroup) = @_;
+
+    my $class = ref($type) || $type;
+
+    my $self = $class->SUPER::new($fname, $ogroup);
+
+    return $self;
+}
+
+sub load_attr {
+    my ($type, $ruledb, $id, $ogroup, $value) = @_;
+
+    my $class = ref($type) || $type;
+
+    my $obj = $class->SUPER::load_attr($ruledb, $id, $ogroup, $value);
+
+    return $obj;
+}
+
+sub parse_entity {
+    my ($self, $entity) = @_;
+
+    my $res;
+
+    if (my $id = $entity->head->mime_attr('x-proxmox-tmp-aid')) {
+	chomp $id;
+
+	my $fn = PMG::Utils::extract_filename($entity->head);
+	if (defined($fn) && $fn =~ m|^$self->{fname}$|i) {
+	    push @$res, $id;
+	} elsif (my $filenames = $entity->{PMX_filenames}) {
+	    # Match inside archives
+	    for my $fn (keys %$filenames) {
+		if ($fn =~ m|^$self->{fname}$|i) {
+		    push @$res, $id;
+		    last;
+		}
+	    }
+	}
+    }
+
+    foreach my $part ($entity->parts)  {
+	if (my $match = $self->parse_entity ($part)) {
+	    push @$res, @$match;
+	}
+    }
+
+    return $res;
+}
+
+sub what_match {
+    my ($self, $queue, $entity, $msginfo) = @_;
+
+    return $self->parse_entity($entity);
+}
+
+sub short_desc {
+    my $self = shift;
+
+    return "filename=$self->{fname}";
+}
+
+sub properties {
+    my ($class) = @_;
+
+    return {
+	filename => {
+	    description => "Filename filter",
+	    type => 'string',
+	    maxLength => 1024,
+	},
+    };
+}
+
+sub get {
+    my ($self) = @_;
+
+    return { filename => $self->{fname} };
+}
+
+sub update {
+    my ($self, $param) = @_;
+
+    $self->{fname} = $param->{filename};
+}
+
+1;
+__END__
+
+=head1 PMG::RuleDB::MatchArchiveFilename
+
+Match Archive Filename
-- 
2.20.1




More information about the pmg-devel mailing list