]> git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Remove.pm
SMTP.pm: SMTPUTF8 is always passed with "mail" command
[pmg-api.git] / PMG / RuleDB / Remove.pm
1 package PMG::RuleDB::Remove;
2
3 use strict;
4 use warnings;
5 use DBI;
6 use Digest::SHA;
7 use MIME::Words;
8 use MIME::Entity;
9 use Encode;
10
11 use PVE::SafeSyslog;
12
13 use PMG::Utils;
14 use PMG::ModGroup;
15 use PMG::RuleDB::Object;
16
17 use base qw(PMG::RuleDB::Object);
18
19 sub otype {
20 return 4007;
21 }
22
23 sub otype_text {
24 return 'Remove attachments';
25 }
26
27 sub oclass {
28 return 'action';
29 }
30
31 sub oisedit {
32 return 1;
33 }
34
35 sub final {
36 return 0;
37 }
38
39 sub priority {
40 return 40;
41 }
42
43 sub new {
44 my ($type, $all, $text, $ogroup) = @_;
45
46 my $class = ref($type) || $type;
47
48 $all = 0 if !defined ($all);
49
50 my $self = $class->SUPER::new($class->otype(), $ogroup);
51
52 $self->{all} = $all;
53 $self->{text} = $text;
54
55 return $self;
56 }
57
58 sub load_attr {
59 my ($type, $ruledb, $id, $ogroup, $value) = @_;
60
61 my $class = ref($type) || $type;
62
63 defined ($value) || die "undefined value: ERROR";
64
65 my $obj;
66
67 if ($value =~ m/^([01])(\:(.*))?$/s) {
68 $obj = $class->new($1, $3, $ogroup);
69 } else {
70 $obj = $class->new(0, undef, $ogroup);
71 }
72
73 $obj->{id} = $id;
74
75 $obj->{digest} = Digest::SHA::sha1_hex($id, $value, $ogroup);
76
77 return $obj;
78 }
79
80 sub save {
81 my ($self, $ruledb) = @_;
82
83 defined($self->{ogroup}) || die "undefined ogroup: ERROR";
84
85 my $value = $self->{all} ? '1' : '0';
86
87 if ($self->{text}) {
88 $value .= ":$self->{text}";
89 }
90
91 if (defined ($self->{id})) {
92 # update
93
94 $ruledb->{dbh}->do(
95 "UPDATE Object SET Value = ? WHERE ID = ?",
96 undef, $value, $self->{id});
97
98 } else {
99 # insert
100
101 my $sth = $ruledb->{dbh}->prepare(
102 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
103 "VALUES (?, ?, ?);");
104
105 $sth->execute($self->ogroup, $self->otype, $value);
106
107 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
108 }
109
110 return $self->{id};
111 }
112
113 sub delete_marked_parts {
114 my ($self, $queue, $entity, $html, $rtype, $marks) = @_;
115
116 my $nparts = [];
117
118 my $pn = $entity->parts;
119 for (my $i = 0; $i < $pn; $i++) {
120 my $part = $entity->parts($i);
121
122 my ($id, $found);
123
124 if ($id = $part->head->mime_attr('x-proxmox-tmp-aid')) {
125 chomp $id;
126
127 if ($self->{all}) {
128 my $ctype_part = $part->head->mime_type;
129 if (!($i == 0 && $ctype_part =~ m|text/.*|i)) {
130 $found = 1;
131 }
132 } else {
133 foreach my $m (@$marks) {
134 $found = 1 if $m eq $id;
135 }
136 }
137
138 }
139
140 if ($found) {
141
142 my $on = PMG::Utils::extract_filename($part->head) || '';
143
144 my $text = PMG::Utils::subst_values($html, { FILENAME => $on } );
145
146 my $fname = "REMOVED_ATTACHMENT_$id." . ($rtype eq "text/html" ? "html" : "txt");
147
148 my $ent = MIME::Entity->build(
149 Type => $rtype,
150 Charset => 'UTF-8',
151 Encoding => "quoted-printable",
152 Filename => $fname,
153 Disposition => "attachment",
154 Data => encode('UTF-8', $text));
155
156 push (@$nparts, $ent);
157
158 syslog ('info', "%s: removed attachment $id ('%s')",
159 $queue->{logid}, $on);
160
161 } else {
162 $self->delete_marked_parts($queue, $part, $html, $rtype, $marks);
163 push (@$nparts, $part);
164 }
165 }
166
167 $entity->parts ($nparts);
168 }
169
170 sub execute {
171 my ($self, $queue, $ruledb, $mod_group, $targets,
172 $msginfo, $vars, $marks) = @_;
173
174 if (!$self->{all} && ($#$marks == -1)) {
175 # no marks
176 return;
177 }
178
179 my $subgroups = $mod_group->subgroups ($targets);
180
181 my $html = PMG::Utils::subst_values($self->{text}, $vars);
182
183 $html = "This attachment was removed: __FILENAME__\n" if !$html;
184
185 my $rtype = "text/plain";
186
187 if ($html =~ m/\<\w+\>/s) {
188 $rtype = "text/html";
189 }
190
191 foreach my $ta (@$subgroups) {
192 my ($tg, $entity) = (@$ta[0], @$ta[1]);
193
194 # handle singlepart mails
195 my $ctype = $entity->head->mime_type;
196 if (!$entity->is_multipart && (!$self->{all} || $ctype !~ m|text/.*|i)) {
197 $entity->make_multipart();
198 my $first_part = $entity->parts(0);
199 $first_part->head->mime_attr('x-proxmox-tmp-aid' => $entity->head->mime_attr('x-proxmox-tmp-aid'));
200 $entity->head->delete('x-proxmox-tmp-aid');
201 }
202
203 $self->delete_marked_parts($queue, $entity, $html, $rtype, $marks);
204
205 if ($msginfo->{testmode}) {
206 $entity->head->mime_attr('Content-type.boundary' => '------=_TEST123456') if $entity->is_multipart;
207 }
208 }
209 }
210
211 sub short_desc {
212 my $self = shift;
213
214 if ($self->{all}) {
215 return "remove all attachments";
216 } else {
217 return "remove matching attachments";
218 }
219 }
220
221 sub properties {
222 my ($class) = @_;
223
224 return {
225 all => {
226 description => "Remove all attachments",
227 type => 'boolean',
228 optional => 1,
229 },
230 text => {
231 description => "The replacement text.",
232 type => 'string',
233 maxLength => 2048
234 }
235 };
236 }
237
238 sub get {
239 my ($self) = @_;
240
241 return {
242 text => $self->{text},
243 all => $self->{all},
244 };
245 }
246
247 sub update {
248 my ($self, $param) = @_;
249
250 $self->{text} = $param->{text};
251 $self->{all} = $param->{all} ? 1 : 0;
252 }
253
254 1;
255 __END__
256
257 =head1 PMG::RuleDB::Remove
258
259 Remove attachments.