]> git.proxmox.com Git - pmg-api.git/commitdiff
add new What Object 'Match Archive Filename'
authorDominik Csapak <d.csapak@proxmox.com>
Thu, 16 Apr 2020 08:59:40 +0000 (10:59 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 16 Apr 2020 13:01:46 +0000 (15:01 +0200)
This behaves like the 'ArchiveFilter' to 'ContentTypeFilter', in that
it matches the filenames in archives, as well as the filenames of
attachments (via filename property in the mime header).

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Reviewed-By: Stoiko Ivanov <s.ivanov@proxmox.com>
Tested-By: Stoiko Ivanov <s.ivanov@proxmox.com>
src/Makefile
src/PMG/API2/What.pm
src/PMG/RuleCache.pm
src/PMG/RuleDB.pm
src/PMG/RuleDB/MatchArchiveFilename.pm [new file with mode: 0644]

index b5e4c9f2413eb2df85eb9de22ea2025980732615..05d9598f732c2937237b6d8a5babb787709b7017 100644 (file)
@@ -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          \
index 14b9053f926412196941770d2d4b209b3d61f8c3..bba3003d6043251d21f70ffce1c2828e5d7d039b 100644 (file)
@@ -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;
index 9b18e7ee80fba14af5efd6aa6cc966bfbaf9607d..655f4a2cf2f193d3ff242afe07cd8cf5d9050bf8 100644 (file)
@@ -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) {
index 9b8abd037a0c48ed1acb07457eac93e8d21fbf80..8b38ac07c4cbc4b0d17b9c0ddab415a7dd7e1184 100644 (file)
@@ -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 (file)
index 0000000..2ef3543
--- /dev/null
@@ -0,0 +1,59 @@
+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 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;
+}
+
+1;
+__END__
+
+=head1 PMG::RuleDB::MatchArchiveFilename
+
+Match Archive Filename