]>
git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Notify.pm
ad166201059cc28f6a37b7badeb93d1630822885
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, $obj->{to
}, $obj->{subject
}, $obj->{body
}, $obj->{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 $self->{attach
} //= 'N';
117 if (defined ($self->{id
})) {
121 $ruledb->{dbh
}->begin_work if !$no_trans;
124 "UPDATE Object SET Value = ? WHERE ID = ?",
125 undef, $self->{to
}, $self->{id
});
128 "UPDATE Attribut SET Value = ? " .
129 "WHERE Name = ? and Object_ID = ?",
130 undef, $self->{subject
}, 'subject', $self->{id
});
133 "UPDATE Attribut SET Value = ? " .
134 "WHERE Name = ? and Object_ID = ?",
135 undef, $self->{body
}, 'body', $self->{id
});
138 "UPDATE Attribut SET Value = ? " .
139 "WHERE Name = ? and Object_ID = ?",
140 undef, $self->{attach
}, 'attach', $self->{id
});
142 $ruledb->{dbh
}->commit if !$no_trans;
145 die $err if !$no_trans;
146 $ruledb->{dbh
}->rollback;
154 $ruledb->{dbh
}->begin_work if !$no_trans;
158 my $sth = $ruledb->{dbh
}->prepare(
159 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
160 "VALUES (?, ?, ?);");
162 $sth->execute($self->ogroup, $self->otype, $self->{to
});
164 $self->{id
} = PMG
::Utils
::lastid
($ruledb->{dbh
}, 'object_id_seq');
168 $ruledb->{dbh
}->do("INSERT INTO Attribut " .
169 "(Object_ID, Name, Value) " .
170 "VALUES (?, ?, ?)", undef,
171 $self->{id
}, 'subject', $self->{subject
});
172 $ruledb->{dbh
}->do("INSERT INTO Attribut " .
173 "(Object_ID, Name, Value) " .
174 "VALUES (?, ?, ?)", undef,
175 $self->{id
}, 'body', $self->{body
});
176 $ruledb->{dbh
}->do("INSERT INTO Attribut " .
177 "(Object_ID, Name, Value) " .
178 "VALUES (?, ?, ?)", undef,
179 $self->{id
}, 'attach', $self->{attach
});
181 $ruledb->{dbh
}->commit if !$no_trans;
184 die $err if !$no_trans;
185 $ruledb->{dbh
}->rollback;
195 my ($self, $queue, $ruledb, $mod_group, $targets,
196 $msginfo, $vars, $marks) = @_;
200 my $from = 'postmaster';
202 my $body = PMG
::Utils
::subst_values
($self->{body
}, $vars);
203 my $subject = PMG
::Utils
::subst_values
($self->{subject
}, $vars);
204 my $to = PMG
::Utils
::subst_values
($self->{to
}, $vars);
206 if ($to =~ m/^\s*$/) {
207 # this happens if a notification is triggered by bounce mails
208 # which notifies the sender <> - we just log and then ignore it
209 syslog
('info', "%s: notify <> (ignored)", $queue->{logid
});
216 my $top = MIME
::Entity-
>build(
222 if ($self->{attach
} eq 'O') {
223 # attach original mail
224 my $spooldir = $PMG::MailQueue
::spooldir
;
225 my $path = "$spooldir/active/$queue->{uid}";
226 $original = $top->attach(
228 Filename
=> "original_message.eml",
229 Type
=> "message/rfc822",);
232 if ($msginfo->{testmode
}) {
233 my $fh = $msginfo->{test_fh
};
234 print $fh "notify: $self->{to}\n";
235 print $fh "notify content:\n";
237 if ($self->{attach
} eq 'O') {
238 # make result reproducable for regression testing
239 $top->head->replace('content-type',
240 'multipart/mixed; boundary="---=_1234567"');
243 print $fh "notify end\n";
245 my @targets = split(/\s*,\s*/, $to);
246 my $qid = PMG
::Utils
::reinject_mail
(
247 $top, $from, \
@targets, undef, $msginfo->{fqdn
});
250 syslog
('info', "%s: notify <%s> (%s)", $queue->{logid
}, $_, $qid);
252 syslog
('err', "%s: notify <%s> failed", $queue->{logid
}, $_);
261 return "notify $self->{to}";
269 description
=> "The Receiver E-Mail address",
274 description
=> "The Notification subject",
279 description
=> "Attach original E-Mail",
285 description
=> "The Notification Body",
297 subject
=> $self->{subject
},
298 body
=> $self->{body
},
299 attach
=> ($self->{attach
} eq 'O') ?
1 : 0,
304 my ($self, $param) = @_;
306 $self->{to
} = $param->{to
};
307 $self->{subject
} = $param->{subject
};
308 $self->{body
} = $param->{body
};
309 $self->{attach
} = $param->{attach
} ?
'O' : 'N';
316 =head1 PMG::RuleDB::Notify