]>
Commit | Line | Data |
---|---|---|
758c7b6b DM |
1 | package PMG::RuleDB::Notify; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
758c7b6b DM |
5 | use DBI; |
6 | use MIME::Body; | |
7 | use MIME::Head; | |
8 | use MIME::Entity; | |
7c142311 | 9 | use Encode qw(decode encode); |
758c7b6b DM |
10 | |
11 | use PVE::SafeSyslog; | |
12 | ||
13 | use PMG::Utils; | |
14 | use PMG::ModGroup; | |
15 | use PMG::RuleDB::Object; | |
74d48a9a | 16 | use PMG::MailQueue; |
758c7b6b DM |
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 | ||
758c7b6b DM |
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 | ||
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; | |
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 | ||
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 | ||
113 | sub 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 | ||
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 $body = PMG::Utils::subst_values($self->{body}, $vars); | |
211 | my $subject = PMG::Utils::subst_values($self->{subject}, $vars); | |
212 | my $to = PMG::Utils::subst_values($self->{to}, $vars); | |
213 | ||
214 | if ($to =~ m/^\s*$/) { | |
215 | # this happens if a notification is triggered by bounce mails | |
216 | # which notifies the sender <> - we just log and then ignore it | |
217 | syslog('info', "%s: notify <> (ignored)", $queue->{logid}); | |
218 | return; | |
219 | } | |
220 | ||
221 | $to =~ s/[;,]/ /g; | |
222 | $to =~ s/\s+/,/g; | |
223 | ||
224 | my $top = MIME::Entity->build( | |
225 | From => $from, | |
226 | To => $to, | |
227 | Subject => $subject, | |
228 | Data => $body); | |
229 | ||
230 | if ($self->{attach} eq 'O') { | |
231 | # attach original mail | |
74d48a9a DM |
232 | my $spooldir = $PMG::MailQueue::spooldir; |
233 | my $path = "$spooldir/active/$queue->{uid}"; | |
758c7b6b DM |
234 | $original = $top->attach( |
235 | Path => $path, | |
236 | Filename => "original_message.eml", | |
237 | Type => "message/rfc822",); | |
238 | } | |
239 | ||
240 | if ($msginfo->{testmode}) { | |
241 | my $fh = $msginfo->{test_fh}; | |
242 | print $fh "notify: $self->{to}\n"; | |
243 | print $fh "notify content:\n"; | |
244 | ||
245 | if ($self->{attach} eq 'O') { | |
246 | # make result reproducable for regression testing | |
247 | $top->head->replace('content-type', | |
248 | 'multipart/mixed; boundary="---=_1234567"'); | |
249 | } | |
250 | $top->print ($fh); | |
251 | print $fh "notify end\n"; | |
252 | } else { | |
253 | my @targets = split(/\s*,\s*/, $to); | |
254 | my $qid = PMG::Utils::reinject_mail( | |
255 | $top, $from, \@targets, undef, $msginfo->{fqdn}); | |
256 | foreach (@targets) { | |
257 | if ($qid) { | |
258 | syslog('info', "%s: notify <%s> (%s)", $queue->{logid}, $_, $qid); | |
259 | } else { | |
260 | syslog ('err', "%s: notify <%s> failed", $queue->{logid}, $_); | |
261 | } | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
758c7b6b DM |
266 | sub short_desc { |
267 | my $self = shift; | |
268 | ||
269 | return "notify $self->{to}"; | |
270 | } | |
271 | ||
4cb22484 DC |
272 | sub properties { |
273 | my ($class) = @_; | |
274 | ||
275 | return { | |
276 | to => { | |
277 | description => "The Receiver E-Mail address", | |
278 | type => 'string', | |
279 | maxLength => 200, | |
280 | }, | |
281 | subject => { | |
282 | description => "The Notification subject", | |
283 | type => 'string', | |
284 | maxLength => 100, | |
285 | }, | |
286 | attach => { | |
287 | description => "Attach original E-Mail", | |
288 | type => 'boolean', | |
289 | optional => 1, | |
290 | default => 0, | |
291 | }, | |
292 | body => { | |
293 | description => "The Notification Body", | |
294 | type => 'string', | |
295 | maxLength => 2048 | |
296 | } | |
297 | }; | |
298 | } | |
299 | ||
300 | sub get { | |
301 | my ($self) = @_; | |
302 | ||
303 | return { | |
304 | to => $self->{to}, | |
305 | subject => $self->{subject}, | |
306 | body => $self->{body}, | |
307 | attach => ($self->{attach} eq 'O') ? 1 : 0, | |
308 | }; | |
309 | } | |
310 | ||
311 | sub update { | |
312 | my ($self, $param) = @_; | |
313 | ||
314 | $self->{to} = $param->{to}; | |
315 | $self->{subject} = $param->{subject}; | |
316 | $self->{body} = $param->{body}; | |
aa376168 | 317 | $self->{attach} = $param->{attach} ? 'O' : 'N'; |
4cb22484 DC |
318 | } |
319 | ||
758c7b6b DM |
320 | 1; |
321 | ||
322 | __END__ | |
323 | ||
324 | =head1 PMG::RuleDB::Notify | |
325 | ||
326 | Notifications. |