]> git.proxmox.com Git - pmg-api.git/blame - PMG/RuleDB/Notify.pm
Add default rulename of 'unknown' to Actions
[pmg-api.git] / 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(
227 From => $from,
228 To => $to,
229 Subject => $subject,
230 Data => $body);
231
232 if ($self->{attach} eq 'O') {
233 # attach original mail
74d48a9a
DM
234 my $spooldir = $PMG::MailQueue::spooldir;
235 my $path = "$spooldir/active/$queue->{uid}";
758c7b6b
DM
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) {
365d5b95 260 syslog('info', "%s: notify <%s> (rule: %s, %s)", $queue->{logid}, $_, $rulename, $qid);
758c7b6b 261 } else {
365d5b95 262 syslog ('err', "%s: notify <%s> (rule: %s) failed", $queue->{logid}, $_, $rulename);
758c7b6b
DM
263 }
264 }
265 }
266}
267
758c7b6b
DM
268sub short_desc {
269 my $self = shift;
270
271 return "notify $self->{to}";
272}
273
4cb22484
DC
274sub 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
302sub 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
313sub update {
314 my ($self, $param) = @_;
315
316 $self->{to} = $param->{to};
317 $self->{subject} = $param->{subject};
318 $self->{body} = $param->{body};
aa376168 319 $self->{attach} = $param->{attach} ? 'O' : 'N';
4cb22484
DC
320}
321
758c7b6b
DM
3221;
323
324__END__
325
326=head1 PMG::RuleDB::Notify
327
328Notifications.