]> git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Notify.pm
20d87afae2607f886b1249717a834c451c71a87f
[pmg-api.git] / 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};
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 From => $from,
228 To => $to,
229 Subject => $subject,
230 Data => $body);
231
232 if ($self->{attach} eq 'O') {
233 # attach original mail
234 my $spooldir = $PMG::MailQueue::spooldir;
235 my $path = "$spooldir/active/$queue->{uid}";
236 $original = $top->attach(
237 Path => $path,
238 Filename => "original_message.eml",
239 Type => "message/rfc822",);
240 }
241
242 if ($msginfo->{testmode}) {
243 my $fh = $msginfo->{test_fh};
244 print $fh "notify: $self->{to}\n";
245 print $fh "notify content:\n";
246
247 if ($self->{attach} eq 'O') {
248 # make result reproducable for regression testing
249 $top->head->replace('content-type',
250 'multipart/mixed; boundary="---=_1234567"');
251 }
252 $top->print ($fh);
253 print $fh "notify end\n";
254 } else {
255 my @targets = split(/\s*,\s*/, $to);
256 my $qid = PMG::Utils::reinject_mail(
257 $top, $from, \@targets, undef, $msginfo->{fqdn});
258 foreach (@targets) {
259 if ($qid) {
260 syslog('info', "%s: notify <%s> (rule: %s, %s)", $queue->{logid}, $_, $rulename, $qid);
261 } else {
262 syslog ('err', "%s: notify <%s> (rule: %s) failed", $queue->{logid}, $_, $rulename);
263 }
264 }
265 }
266 }
267
268 sub short_desc {
269 my $self = shift;
270
271 return "notify $self->{to}";
272 }
273
274 sub properties {
275 my ($class) = @_;
276
277 return {
278 to => {
279 description => "The Receiver E-Mail address",
280 type => 'string',
281 maxLength => 200,
282 },
283 subject => {
284 description => "The Notification subject",
285 type => 'string',
286 maxLength => 100,
287 },
288 attach => {
289 description => "Attach original E-Mail",
290 type => 'boolean',
291 optional => 1,
292 default => 0,
293 },
294 body => {
295 description => "The Notification Body",
296 type => 'string',
297 maxLength => 2048
298 }
299 };
300 }
301
302 sub get {
303 my ($self) = @_;
304
305 return {
306 to => $self->{to},
307 subject => $self->{subject},
308 body => $self->{body},
309 attach => ($self->{attach} eq 'O') ? 1 : 0,
310 };
311 }
312
313 sub update {
314 my ($self, $param) = @_;
315
316 $self->{to} = $param->{to};
317 $self->{subject} = $param->{subject};
318 $self->{body} = $param->{body};
319 $self->{attach} = $param->{attach} ? 'O' : 'N';
320 }
321
322 1;
323
324 __END__
325
326 =head1 PMG::RuleDB::Notify
327
328 Notifications.