]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/RuleDB/Notify.pm
fix #2525: encode notifications in UTF-8
[pmg-api.git] / src / PMG / RuleDB / Notify.pm
1 package PMG::RuleDB::Notify;
2
3 use strict;
4 use warnings;
5 use DBI;
6 use MIME::Body;
7 use MIME::Head;
8 use MIME::Entity;
9 use Encode qw(decode encode);
10
11 use PVE::SafeSyslog;
12
13 use PMG::Utils;
14 use PMG::ModGroup;
15 use PMG::RuleDB::Object;
16 use PMG::MailQueue;
17
18 use base qw(PMG::RuleDB::Object);
19
20 sub otype {
21 return 4002;
22 }
23
24 sub oclass {
25 return 'action';
26 }
27
28 sub otype_text {
29 return 'Notification';
30 }
31
32 sub final {
33 return 0;
34 }
35
36 sub priority {
37 return 89;
38 }
39
40 sub new {
41 my ($type, $to, $subject, $body, $attach, $ogroup) = @_;
42
43 my $class = ref($type) || $type;
44
45 my $self = $class->SUPER::new($class->otype(), $ogroup);
46
47 $to //= '__ADMIN__';
48 $attach //= 'N';
49 $subject //= 'Notification: __SUBJECT__';
50
51 if (!defined($body)) {
52 $body = <<EOB;
53 Proxmox Notification:
54
55 Sender: __SENDER__
56 Receiver: __RECEIVERS__
57 Targets: __TARGETS__
58
59 Subject: __SUBJECT__
60
61 Matching Rule: __RULE__
62
63 __RULE_INFO__
64
65 __VIRUS_INFO__
66 __SPAM_INFO__
67 EOB
68 }
69 $self->{to} = $to;
70 $self->{subject} = $subject;
71 $self->{body} = $body;
72 $self->{attach} = $attach;
73
74 return $self;
75 }
76
77 sub load_attr {
78 my ($type, $ruledb, $id, $ogroup, $value) = @_;
79
80 my $class = ref($type) || $type;
81
82 defined($value) || die "undefined object attribute: ERROR";
83
84 my ($raw_subject, $raw_body, $attach);
85
86 my $sth = $ruledb->{dbh}->prepare(
87 "SELECT * FROM Attribut WHERE Object_ID = ?");
88
89 $sth->execute($id);
90
91 while (my $ref = $sth->fetchrow_hashref()) {
92 $raw_subject = $ref->{value} if $ref->{name} eq 'subject';
93 $raw_body = $ref->{value} if $ref->{name} eq 'body';
94 $attach = $ref->{value} if $ref->{name} eq 'attach';
95 }
96
97 $sth->finish();
98
99 # Note: db stores binary (ascii) data, so we need to convert to UTF-8 manually
100 my $obj = $class->new($value,
101 decode('UTF-8', $raw_subject),
102 decode('UTF-8', $raw_body),
103 $attach, $ogroup);
104 $obj->{id} = $id;
105
106 # Note: Digest::SHA does not work with wide UTF8 characters
107 $obj->{digest} = Digest::SHA::sha1_hex(
108 $id, $obj->{to}, $raw_subject, $raw_body, $obj->{attach}, $ogroup);
109
110 return $obj;
111 }
112
113 sub save {
114 my ($self, $ruledb, $no_trans) = @_;
115
116 defined($self->{ogroup}) || die "undefined object attribute: ERROR";
117 defined($self->{to}) || die "undefined object attribute: ERROR";
118 defined($self->{subject}) || die "undefined object attribute: ERROR";
119 defined($self->{body}) || die "undefined object attribute: ERROR";
120
121 $self->{attach} //= 'N';
122
123 # Note: db stores binary (ascii) data, so we need to convert from UTF-8 manually
124
125 if (defined ($self->{id})) {
126 # update
127
128 eval {
129 $ruledb->{dbh}->begin_work if !$no_trans;
130
131 $ruledb->{dbh}->do(
132 "UPDATE Object SET Value = ? WHERE ID = ?",
133 undef, $self->{to}, $self->{id});
134
135 $ruledb->{dbh}->do(
136 "UPDATE Attribut SET Value = ? " .
137 "WHERE Name = ? and Object_ID = ?",
138 undef, encode('UTF-8', $self->{subject}), 'subject', $self->{id});
139
140 $ruledb->{dbh}->do(
141 "UPDATE Attribut SET Value = ? " .
142 "WHERE Name = ? and Object_ID = ?",
143 undef, encode('UTF-8', $self->{body}), 'body', $self->{id});
144
145 $ruledb->{dbh}->do(
146 "UPDATE Attribut SET Value = ? " .
147 "WHERE Name = ? and Object_ID = ?",
148 undef, $self->{attach}, 'attach', $self->{id});
149
150 $ruledb->{dbh}->commit if !$no_trans;
151 };
152 if (my $err = $@) {
153 die $err if !$no_trans;
154 $ruledb->{dbh}->rollback;
155 syslog('err', $err);
156 return undef;
157 }
158
159 } else {
160 # insert
161
162 $ruledb->{dbh}->begin_work if !$no_trans;
163
164 eval {
165
166 my $sth = $ruledb->{dbh}->prepare(
167 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
168 "VALUES (?, ?, ?);");
169
170 $sth->execute($self->ogroup, $self->otype, $self->{to});
171
172 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
173
174 $sth->finish();
175
176 $ruledb->{dbh}->do("INSERT INTO Attribut " .
177 "(Object_ID, Name, Value) " .
178 "VALUES (?, ?, ?)", undef,
179 $self->{id}, 'subject', encode('UTF-8', $self->{subject}));
180 $ruledb->{dbh}->do("INSERT INTO Attribut " .
181 "(Object_ID, Name, Value) " .
182 "VALUES (?, ?, ?)", undef,
183 $self->{id}, 'body', encode('UTF-8', $self->{body}));
184 $ruledb->{dbh}->do("INSERT INTO Attribut " .
185 "(Object_ID, Name, Value) " .
186 "VALUES (?, ?, ?)", undef,
187 $self->{id}, 'attach', $self->{attach});
188
189 $ruledb->{dbh}->commit if !$no_trans;
190 };
191 if (my $err = $@) {
192 die $err if !$no_trans;
193 $ruledb->{dbh}->rollback;
194 syslog('err', $err);
195 return undef;
196 }
197 }
198
199 return $self->{id};
200 }
201
202 sub execute {
203 my ($self, $queue, $ruledb, $mod_group, $targets,
204 $msginfo, $vars, $marks) = @_;
205
206 my $original;
207
208 my $from = 'postmaster';
209
210 my $rulename = $vars->{RULE} // 'unknown';
211
212 my $body = PMG::Utils::subst_values($self->{body}, $vars);
213 my $subject = PMG::Utils::subst_values($self->{subject}, $vars);
214 my $to = PMG::Utils::subst_values($self->{to}, $vars);
215
216 if ($to =~ m/^\s*$/) {
217 # this happens if a notification is triggered by bounce mails
218 # which notifies the sender <> - we just log and then ignore it
219 syslog('info', "%s: notify <> (rule: %s, ignored)", $queue->{logid}, $rulename);
220 return;
221 }
222
223 $to =~ s/[;,]/ /g;
224 $to =~ s/\s+/,/g;
225
226 my $top = MIME::Entity->build(
227 Encoding => 'quoted-printable',
228 Charset => 'UTF-8',
229 From => $from,
230 To => $to,
231 Subject => encode('UTF-8', $subject),
232 Data => encode('UTF-8', $body));
233
234 if ($self->{attach} eq 'O') {
235 # attach original mail
236 my $spooldir = $PMG::MailQueue::spooldir;
237 my $path = "$spooldir/active/$queue->{uid}";
238 $original = $top->attach(
239 Path => $path,
240 Filename => "original_message.eml",
241 Type => "message/rfc822",);
242 }
243
244 if ($msginfo->{testmode}) {
245 my $fh = $msginfo->{test_fh};
246 print $fh "notify: $self->{to}\n";
247 print $fh "notify content:\n";
248
249 if ($self->{attach} eq 'O') {
250 # make result reproducable for regression testing
251 $top->head->replace('content-type',
252 'multipart/mixed; boundary="---=_1234567"');
253 }
254 $top->print ($fh);
255 print $fh "notify end\n";
256 } else {
257 my @targets = split(/\s*,\s*/, $to);
258 my $qid = PMG::Utils::reinject_mail(
259 $top, $from, \@targets, undef, $msginfo->{fqdn});
260 foreach (@targets) {
261 if ($qid) {
262 syslog('info', "%s: notify <%s> (rule: %s, %s)", $queue->{logid}, $_, $rulename, $qid);
263 } else {
264 syslog ('err', "%s: notify <%s> (rule: %s) failed", $queue->{logid}, $_, $rulename);
265 }
266 }
267 }
268 }
269
270 sub short_desc {
271 my $self = shift;
272
273 return "notify $self->{to}";
274 }
275
276 sub properties {
277 my ($class) = @_;
278
279 return {
280 to => {
281 description => "The Receiver E-Mail address",
282 type => 'string',
283 maxLength => 200,
284 },
285 subject => {
286 description => "The Notification subject",
287 type => 'string',
288 maxLength => 100,
289 },
290 attach => {
291 description => "Attach original E-Mail",
292 type => 'boolean',
293 optional => 1,
294 default => 0,
295 },
296 body => {
297 description => "The Notification Body",
298 type => 'string',
299 maxLength => 2048
300 }
301 };
302 }
303
304 sub get {
305 my ($self) = @_;
306
307 return {
308 to => $self->{to},
309 subject => $self->{subject},
310 body => $self->{body},
311 attach => ($self->{attach} eq 'O') ? 1 : 0,
312 };
313 }
314
315 sub update {
316 my ($self, $param) = @_;
317
318 $self->{to} = $param->{to};
319 $self->{subject} = $param->{subject};
320 $self->{body} = $param->{body};
321 $self->{attach} = $param->{attach} ? 'O' : 'N';
322 }
323
324 1;
325
326 __END__
327
328 =head1 PMG::RuleDB::Notify
329
330 Notifications.