]>
git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Notify.pm
1 package PMG
::RuleDB
::Notify
;
14 use PMG
::RuleDB
::Object
;
17 use base
qw(PMG::RuleDB::Object);
28 return 'Notification';
40 my ($type, $to, $subject, $body, $attach, $ogroup) = @_;
42 my $class = ref($type) || $type;
44 my $self = $class->SUPER::new
($class->otype(), $ogroup);
48 $subject //= 'Notification: __SUBJECT__';
50 if (!defined($body)) {
55 Receiver: __RECEIVERS__
60 Matching Rule: __RULE__
69 $self->{subject
} = $subject;
70 $self->{body
} = $body;
71 $self->{attach
} = $attach;
77 my ($type, $ruledb, $id, $ogroup, $value) = @_;
79 my $class = ref($type) || $type;
81 defined($value) || die "undefined object attribute: ERROR";
83 my ($subject, $body, $attach);
85 my $sth = $ruledb->{dbh
}->prepare(
86 "SELECT * FROM Attribut WHERE Object_ID = ?");
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';
98 my $obj = $class->new($value, $subject, $body, $attach, $ogroup);
101 $obj->{digest
} = Digest
::SHA
::sha1_hex
(
102 $id, $value, $subject, $body, $attach, $ogroup);
108 my ($self, $ruledb, $no_trans) = @_;
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";
115 if (defined ($self->{id
})) {
119 $ruledb->{dbh
}->begin_work if !$no_trans;
122 "UPDATE Object SET Value = ? WHERE ID = ?",
123 undef, $self->{to
}, $self->{id
});
126 "UPDATE Attribut SET Value = ? " .
127 "WHERE Name = ? and Object_ID = ?",
128 undef, $self->{subject
}, 'subject', $self->{id
});
131 "UPDATE Attribut SET Value = ? " .
132 "WHERE Name = ? and Object_ID = ?",
133 undef, $self->{body
}, 'body', $self->{id
});
136 "UPDATE Attribut SET Value = ? " .
137 "WHERE Name = ? and Object_ID = ?",
138 undef, $self->{attach
}, 'attach', $self->{id
});
140 $ruledb->{dbh
}->commit if !$no_trans;
143 die $err if !$no_trans;
144 $ruledb->{dbh
}->rollback;
152 $ruledb->{dbh
}->begin_work if !$no_trans;
156 my $sth = $ruledb->{dbh
}->prepare(
157 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
158 "VALUES (?, ?, ?);");
160 $sth->execute($self->ogroup, $self->otype, $self->{to
});
162 $self->{id
} = PMG
::Utils
::lastid
($ruledb->{dbh
}, 'object_id_seq');
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
});
179 $ruledb->{dbh
}->commit if !$no_trans;
182 die $err if !$no_trans;
183 $ruledb->{dbh
}->rollback;
193 my ($self, $queue, $ruledb, $mod_group, $targets,
194 $msginfo, $vars, $marks) = @_;
198 my $from = 'postmaster';
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);
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
});
214 my $top = MIME
::Entity-
>build(
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(
226 Filename
=> "original_message.eml",
227 Type
=> "message/rfc822",);
230 if ($msginfo->{testmode
}) {
231 my $fh = $msginfo->{test_fh
};
232 print $fh "notify: $self->{to}\n";
233 print $fh "notify content:\n";
235 if ($self->{attach
} eq 'O') {
236 # make result reproducable for regression testing
237 $top->head->replace('content-type',
238 'multipart/mixed; boundary="---=_1234567"');
241 print $fh "notify end\n";
243 my @targets = split(/\s*,\s*/, $to);
244 my $qid = PMG
::Utils
::reinject_mail
(
245 $top, $from, \
@targets, undef, $msginfo->{fqdn
});
248 syslog
('info', "%s: notify <%s> (%s)", $queue->{logid
}, $_, $qid);
250 syslog
('err', "%s: notify <%s> failed", $queue->{logid
}, $_);
259 return "notify $self->{to}";
267 description
=> "The Receiver E-Mail address",
272 description
=> "The Notification subject",
277 description
=> "Attach original E-Mail",
283 description
=> "The Notification Body",
295 subject
=> $self->{subject
},
296 body
=> $self->{body
},
297 attach
=> ($self->{attach
} eq 'O') ?
1 : 0,
302 my ($self, $param) = @_;
304 $self->{to
} = $param->{to
};
305 $self->{subject
} = $param->{subject
};
306 $self->{body
} = $param->{body
};
307 $self->{attach
} = $param->{attach
} ?
'O' : undef;
314 =head1 PMG::RuleDB::Notify