]> git.proxmox.com Git - pmg-api.git/blame - src/PMG/RuleDB/Notify.pm
fix #2525: encode notifications in UTF-8
[pmg-api.git] / src / PMG / RuleDB / Notify.pm
CommitLineData
758c7b6b
DM
1package PMG::RuleDB::Notify;
2
3use strict;
4use warnings;
758c7b6b
DM
5use DBI;
6use MIME::Body;
7use MIME::Head;
8use MIME::Entity;
7c142311 9use Encode qw(decode encode);
758c7b6b
DM
10
11use PVE::SafeSyslog;
12
13use PMG::Utils;
14use PMG::ModGroup;
15use PMG::RuleDB::Object;
74d48a9a 16use PMG::MailQueue;
758c7b6b
DM
17
18use base qw(PMG::RuleDB::Object);
19
20sub otype {
21 return 4002;
22}
23
24sub oclass {
25 return 'action';
26}
27
28sub otype_text {
29 return 'Notification';
30}
31
758c7b6b
DM
32sub final {
33 return 0;
34}
35
36sub priority {
37 return 89;
38}
39
40sub new {
41 my ($type, $to, $subject, $body, $attach, $ogroup) = @_;
42
43 my $class = ref($type) || $type;
44
7a2cf7e6 45 my $self = $class->SUPER::new($class->otype(), $ogroup);
758c7b6b
DM
46
47 $to //= '__ADMIN__';
48 $attach //= 'N';
49 $subject //= 'Notification: __SUBJECT__';
50
51 if (!defined($body)) {
52 $body = <<EOB;
53Proxmox Notification:
54
55Sender: __SENDER__
56Receiver: __RECEIVERS__
57Targets: __TARGETS__
58
59Subject: __SUBJECT__
60
61Matching Rule: __RULE__
62
63__RULE_INFO__
64
65__VIRUS_INFO__
66__SPAM_INFO__
67EOB
68 }
69 $self->{to} = $to;
70 $self->{subject} = $subject;
71 $self->{body} = $body;
72 $self->{attach} = $attach;
73
74 return $self;
75}
76
77sub load_attr {
78 my ($type, $ruledb, $id, $ogroup, $value) = @_;
79
80 my $class = ref($type) || $type;
81
9ef3f143 82 defined($value) || die "undefined object attribute: ERROR";
758c7b6b 83
7c142311 84 my ($raw_subject, $raw_body, $attach);
758c7b6b
DM
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()) {
7c142311
DM
92 $raw_subject = $ref->{value} if $ref->{name} eq 'subject';
93 $raw_body = $ref->{value} if $ref->{name} eq 'body';
758c7b6b
DM
94 $attach = $ref->{value} if $ref->{name} eq 'attach';
95 }
96
97 $sth->finish();
7c142311
DM
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);
758c7b6b
DM
104 $obj->{id} = $id;
105
7c142311 106 # Note: Digest::SHA does not work with wide UTF8 characters
758c7b6b 107 $obj->{digest} = Digest::SHA::sha1_hex(
7c142311 108 $id, $obj->{to}, $raw_subject, $raw_body, $obj->{attach}, $ogroup);
758c7b6b
DM
109
110 return $obj;
111}
112
113sub save {
114 my ($self, $ruledb, $no_trans) = @_;
115
9ef3f143
DM
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";
758c7b6b 120
aa376168
DM
121 $self->{attach} //= 'N';
122
7c142311
DM
123 # Note: db stores binary (ascii) data, so we need to convert from UTF-8 manually
124
758c7b6b
DM
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 = ?",
7c142311 138 undef, encode('UTF-8', $self->{subject}), 'subject', $self->{id});
758c7b6b
DM
139
140 $ruledb->{dbh}->do(
141 "UPDATE Attribut SET Value = ? " .
142 "WHERE Name = ? and Object_ID = ?",
7c142311 143 undef, encode('UTF-8', $self->{body}), 'body', $self->{id});
758c7b6b
DM
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,
7c142311 179 $self->{id}, 'subject', encode('UTF-8', $self->{subject}));
758c7b6b
DM
180 $ruledb->{dbh}->do("INSERT INTO Attribut " .
181 "(Object_ID, Name, Value) " .
182 "VALUES (?, ?, ?)", undef,
7c142311 183 $self->{id}, 'body', encode('UTF-8', $self->{body}));
758c7b6b
DM
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
202sub execute {
203 my ($self, $queue, $ruledb, $mod_group, $targets,
204 $msginfo, $vars, $marks) = @_;
205
206 my $original;
207
208 my $from = 'postmaster';
209
6c65ab40 210 my $rulename = $vars->{RULE} // 'unknown';
365d5b95 211
758c7b6b
DM
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
365d5b95 219 syslog('info', "%s: notify <> (rule: %s, ignored)", $queue->{logid}, $rulename);
758c7b6b
DM
220 return;
221 }
222
223 $to =~ s/[;,]/ /g;
224 $to =~ s/\s+/,/g;
225
226 my $top = MIME::Entity->build(
45102dfd
SI
227 Encoding => 'quoted-printable',
228 Charset => 'UTF-8',
758c7b6b
DM
229 From => $from,
230 To => $to,
45102dfd
SI
231 Subject => encode('UTF-8', $subject),
232 Data => encode('UTF-8', $body));
758c7b6b
DM
233
234 if ($self->{attach} eq 'O') {
235 # attach original mail
74d48a9a
DM
236 my $spooldir = $PMG::MailQueue::spooldir;
237 my $path = "$spooldir/active/$queue->{uid}";
758c7b6b
DM
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) {
365d5b95 262 syslog('info', "%s: notify <%s> (rule: %s, %s)", $queue->{logid}, $_, $rulename, $qid);
758c7b6b 263 } else {
365d5b95 264 syslog ('err', "%s: notify <%s> (rule: %s) failed", $queue->{logid}, $_, $rulename);
758c7b6b
DM
265 }
266 }
267 }
268}
269
758c7b6b
DM
270sub short_desc {
271 my $self = shift;
272
273 return "notify $self->{to}";
274}
275
4cb22484
DC
276sub 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
304sub 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
315sub update {
316 my ($self, $param) = @_;
317
318 $self->{to} = $param->{to};
319 $self->{subject} = $param->{subject};
320 $self->{body} = $param->{body};
aa376168 321 $self->{attach} = $param->{attach} ? 'O' : 'N';
4cb22484
DC
322}
323
758c7b6b
DM
3241;
325
326__END__
327
328=head1 PMG::RuleDB::Notify
329
330Notifications.