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