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