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