]>
git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Notify.pm
1 package PMG
::RuleDB
::Notify
;
15 use PMG
::RuleDB
::Object
;
17 use base
qw(PMG::RuleDB::Object);
28 return 'Notification';
36 return 'Send a notification Mail';
48 my ($type, $to, $subject, $body, $attach, $ogroup) = @_;
50 my $class = ref($type) || $type;
52 my $self = $class->SUPER::new
(otype
(), $ogroup);
56 $subject //= 'Notification: __SUBJECT__';
58 if (!defined($body)) {
63 Receiver: __RECEIVERS__
68 Matching Rule: __RULE__
77 $self->{subject
} = $subject;
78 $self->{body
} = $body;
79 $self->{attach
} = $attach;
85 my ($type, $ruledb, $id, $ogroup, $value) = @_;
87 my $class = ref($type) || $type;
89 defined($value) || croak
"undefined object attribute: ERROR";
91 my ($subject, $body, $attach);
93 my $sth = $ruledb->{dbh
}->prepare(
94 "SELECT * FROM Attribut WHERE Object_ID = ?");
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';
106 my $obj = $class->new($value, $subject, $body, $attach, $ogroup);
109 $obj->{digest
} = Digest
::SHA
::sha1_hex
(
110 $id, $value, $subject, $body, $attach, $ogroup);
116 my ($self, $ruledb, $no_trans) = @_;
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";
123 if (defined ($self->{id
})) {
127 $ruledb->{dbh
}->begin_work if !$no_trans;
130 "UPDATE Object SET Value = ? WHERE ID = ?",
131 undef, $self->{to
}, $self->{id
});
134 "UPDATE Attribut SET Value = ? " .
135 "WHERE Name = ? and Object_ID = ?",
136 undef, $self->{subject
}, 'subject', $self->{id
});
139 "UPDATE Attribut SET Value = ? " .
140 "WHERE Name = ? and Object_ID = ?",
141 undef, $self->{body
}, 'body', $self->{id
});
144 "UPDATE Attribut SET Value = ? " .
145 "WHERE Name = ? and Object_ID = ?",
146 undef, $self->{attach
}, 'attach', $self->{id
});
148 $ruledb->{dbh
}->commit if !$no_trans;
151 die $err if !$no_trans;
152 $ruledb->{dbh
}->rollback;
160 $ruledb->{dbh
}->begin_work if !$no_trans;
164 my $sth = $ruledb->{dbh
}->prepare(
165 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
166 "VALUES (?, ?, ?);");
168 $sth->execute($self->ogroup, $self->otype, $self->{to
});
170 $self->{id
} = PMG
::Utils
::lastid
($ruledb->{dbh
}, 'object_id_seq');
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
});
187 $ruledb->{dbh
}->commit if !$no_trans;
190 die $err if !$no_trans;
191 $ruledb->{dbh
}->rollback;
201 my ($self, $queue, $ruledb, $mod_group, $targets,
202 $msginfo, $vars, $marks) = @_;
206 my $from = 'postmaster';
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);
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
});
222 my $top = MIME
::Entity-
>build(
228 if ($self->{attach
} eq 'O') {
229 # attach original mail
230 my $path = "/var/spool/proxmox/active/$queue->{uid}";
231 $original = $top->attach(
233 Filename
=> "original_message.eml",
234 Type
=> "message/rfc822",);
237 if ($msginfo->{testmode
}) {
238 my $fh = $msginfo->{test_fh
};
239 print $fh "notify: $self->{to}\n";
240 print $fh "notify content:\n";
242 if ($self->{attach
} eq 'O') {
243 # make result reproducable for regression testing
244 $top->head->replace('content-type',
245 'multipart/mixed; boundary="---=_1234567"');
248 print $fh "notify end\n";
250 my @targets = split(/\s*,\s*/, $to);
251 my $qid = PMG
::Utils
::reinject_mail
(
252 $top, $from, \
@targets, undef, $msginfo->{fqdn
});
255 syslog
('info', "%s: notify <%s> (%s)", $queue->{logid
}, $_, $qid);
257 syslog
('err', "%s: notify <%s> failed", $queue->{logid
}, $_);
277 $self->{subject
} = $v;
297 $self->{attach
} = $v;
306 return "notify $self->{to}";
313 =head1 PMG::RuleDB::Notify