]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/RuleDB/Remove.pm
1 package PMG
::RuleDB
::Remove
;
15 use PMG
::RuleDB
::Object
;
17 use base
qw(PMG::RuleDB::Object);
24 return 'Remove attachments';
44 my ($type, $all, $text, $ogroup, $quarantine) = @_;
46 my $class = ref($type) || $type;
48 $all = 0 if !defined ($all);
50 my $self = $class->SUPER::new
($class->otype(), $ogroup);
53 $self->{text
} = $text;
54 $self->{quarantine
} = $quarantine;
60 my ($type, $ruledb, $id, $ogroup, $value) = @_;
62 my $class = ref($type) || $type;
64 defined ($value) || die "undefined value: ERROR";
68 if ($value =~ m/^([01])\,([01])(\:(.*))?$/s) {
69 $obj = $class->new($1, $4, $ogroup, $2);
70 } elsif ($value =~ m/^([01])(\:(.*))?$/s) {
71 $obj = $class->new($1, $3, $ogroup);
73 $obj = $class->new(0, undef, $ogroup);
78 $obj->{digest
} = Digest
::SHA
::sha1_hex
($id, $value, $ogroup);
84 my ($self, $ruledb) = @_;
86 defined($self->{ogroup
}) || die "undefined ogroup: ERROR";
88 my $value = $self->{all
} ?
'1' : '0';
89 $value .= ','. ($self->{quarantine
} ?
'1' : '0');
92 $value .= ":$self->{text}";
95 if (defined ($self->{id
})) {
99 "UPDATE Object SET Value = ? WHERE ID = ?",
100 undef, $value, $self->{id
});
105 my $sth = $ruledb->{dbh
}->prepare(
106 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
107 "VALUES (?, ?, ?);");
109 $sth->execute($self->ogroup, $self->otype, $value);
111 $self->{id
} = PMG
::Utils
::lastid
($ruledb->{dbh
}, 'object_id_seq');
117 sub delete_marked_parts
{
118 my ($self, $queue, $entity, $html, $rtype, $marks, $rulename) = @_;
122 my $ctype = $entity->head->mime_type;
123 my $pn = $entity->parts;
124 for (my $i = 0; $i < $pn; $i++) {
125 my $part = $entity->parts($i);
129 if ($id = $part->head->mime_attr('x-proxmox-tmp-aid')) {
133 my $ctype_part = $part->head->mime_type;
134 if ($self->{message_seen
}) {
137 if ($ctype =~ m
|multipart
/alternative
|i
) {
138 if ($ctype_part !~ m{text/(?:plain|html)}i) {
143 # we have not seen the message and it is the
144 # end of the first multipart/alternative, mark as message seen
145 $self->{message_seen
} = 1;
148 if ($ctype_part =~ m{text/(?:plain|html)}i) {
149 $self->{message_seen
} = 1;
150 } elsif ($ctype_part !~ m
|multipart
/|i
) {
156 foreach my $m (@$marks) {
157 $found = 1 if $m eq $id;
165 my $on = PMG
::Utils
::extract_filename
($part->head) || '';
167 my $text = PMG
::Utils
::subst_values
($html, { FILENAME
=> $on } );
169 my $fname = "REMOVED_ATTACHMENT_$id." . ($rtype eq "text/html" ?
"html" : "txt");
171 my $ent = MIME
::Entity-
>build(
174 Encoding
=> "quoted-printable",
176 Disposition
=> "attachment",
177 Data
=> encode
('UTF-8', $text));
179 push (@$nparts, $ent);
181 syslog
('info', "%s: removed attachment $id ('%s', rule: %s)",
182 $queue->{logid
}, $on, $rulename);
185 $self->delete_marked_parts($queue, $part, $html, $rtype, $marks, $rulename);
186 push (@$nparts, $part);
190 $entity->parts ($nparts);
194 my ($self, $queue, $ruledb, $mod_group, $targets,
195 $msginfo, $vars, $marks, $ldap) = @_;
197 my $rulename = $vars->{RULE
} // 'unknown';
199 if (!$self->{all
} && ($#$marks == -1)) {
204 my $subgroups = $mod_group->subgroups ($targets);
206 my $html = PMG
::Utils
::subst_values
($self->{text
}, $vars);
209 $html = "This attachment was removed: __FILENAME__\n";
210 $html .= "It was put into the Attachment Quarantine, please contact your Administrator\n" if $self->{quarantine
};
213 my $rtype = "text/plain";
215 if ($html =~ m/\<\w+\>/s) {
216 $rtype = "text/html";
219 foreach my $ta (@$subgroups) {
220 my ($tg, $entity) = (@$ta[0], @$ta[1]);
222 # copy original entity to attachment quarantine if configured
223 if ($self->{quarantine
}) {
224 my $original_entity = $entity->dup;
225 PMG
::Utils
::remove_marks
($original_entity);
226 if (my $qid = $queue->quarantine_mail($ruledb, 'A', $original_entity, $tg, $msginfo, $vars, $ldap)) {
228 syslog
('info', "$queue->{logid}: moved mail for <%s> to attachment quarantine - %s (rule: %s)", $_, $qid, $rulename);
233 # handle singlepart mails
234 my $ctype = $entity->head->mime_type;
235 if (!$entity->is_multipart && (!$self->{all
} || $ctype !~ m
|text
/.*|i
)) {
236 $entity->make_multipart();
237 my $first_part = $entity->parts(0);
238 $first_part->head->mime_attr('x-proxmox-tmp-aid' => $entity->head->mime_attr('x-proxmox-tmp-aid'));
239 $entity->head->delete('x-proxmox-tmp-aid');
242 $self->{message_seen
} = 0;
243 $self->delete_marked_parts($queue, $entity, $html, $rtype, $marks, $rulename);
244 delete $self->{message_seen
};
246 if ($msginfo->{testmode
}) {
247 $entity->head->mime_attr('Content-type.boundary' => '------=_TEST123456') if $entity->is_multipart;
256 return "remove all attachments";
258 return "remove matching attachments";
267 description
=> "Remove all attachments",
272 description
=> "Copy original mail to attachment Quarantine.",
278 description
=> "The replacement text.",
289 text
=> $self->{text
},
291 quarantine
=> $self->{quarantine
},
296 my ($self, $param) = @_;
298 $self->{text
} = $param->{text
};
299 $self->{all
} = $param->{all
} ?
1 : 0;
300 $self->{quarantine
} = $param->{quarantine
} ?
1 : 0;
306 =head1 PMG::RuleDB::Remove