]>
git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Notify.pm
1 package PMG
::RuleDB
::Notify
;
9 use Encode
qw(decode encode);
15 use PMG
::RuleDB
::Object
;
18 use base
qw(PMG::RuleDB::Object);
29 return 'Notification';
41 my ($type, $to, $subject, $body, $attach, $ogroup) = @_;
43 my $class = ref($type) || $type;
45 my $self = $class->SUPER::new
($class->otype(), $ogroup);
49 $subject //= 'Notification: __SUBJECT__';
51 if (!defined($body)) {
56 Receiver: __RECEIVERS__
61 Matching Rule: __RULE__
70 $self->{subject
} = $subject;
71 $self->{body
} = $body;
72 $self->{attach
} = $attach;
78 my ($type, $ruledb, $id, $ogroup, $value) = @_;
80 my $class = ref($type) || $type;
82 defined($value) || die "undefined object attribute: ERROR";
84 my ($raw_subject, $raw_body, $attach);
86 my $sth = $ruledb->{dbh
}->prepare(
87 "SELECT * FROM Attribut WHERE Object_ID = ?");
91 while (my $ref = $sth->fetchrow_hashref()) {
92 $raw_subject = $ref->{value
} if $ref->{name
} eq 'subject';
93 $raw_body = $ref->{value
} if $ref->{name
} eq 'body';
94 $attach = $ref->{value
} if $ref->{name
} eq 'attach';
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),
106 # Note: Digest::SHA does not work with wide UTF8 characters
107 $obj->{digest
} = Digest
::SHA
::sha1_hex
(
108 $id, $obj->{to
}, $raw_subject, $raw_body, $obj->{attach
}, $ogroup);
114 my ($self, $ruledb, $no_trans) = @_;
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";
121 $self->{attach
} //= 'N';
123 # Note: db stores binary (ascii) data, so we need to convert from UTF-8 manually
125 if (defined ($self->{id
})) {
129 $ruledb->{dbh
}->begin_work if !$no_trans;
132 "UPDATE Object SET Value = ? WHERE ID = ?",
133 undef, $self->{to
}, $self->{id
});
136 "UPDATE Attribut SET Value = ? " .
137 "WHERE Name = ? and Object_ID = ?",
138 undef, encode
('UTF-8', $self->{subject
}), 'subject', $self->{id
});
141 "UPDATE Attribut SET Value = ? " .
142 "WHERE Name = ? and Object_ID = ?",
143 undef, encode
('UTF-8', $self->{body
}), 'body', $self->{id
});
146 "UPDATE Attribut SET Value = ? " .
147 "WHERE Name = ? and Object_ID = ?",
148 undef, $self->{attach
}, 'attach', $self->{id
});
150 $ruledb->{dbh
}->commit if !$no_trans;
153 die $err if !$no_trans;
154 $ruledb->{dbh
}->rollback;
162 $ruledb->{dbh
}->begin_work if !$no_trans;
166 my $sth = $ruledb->{dbh
}->prepare(
167 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
168 "VALUES (?, ?, ?);");
170 $sth->execute($self->ogroup, $self->otype, $self->{to
});
172 $self->{id
} = PMG
::Utils
::lastid
($ruledb->{dbh
}, 'object_id_seq');
176 $ruledb->{dbh
}->do("INSERT INTO Attribut " .
177 "(Object_ID, Name, Value) " .
178 "VALUES (?, ?, ?)", undef,
179 $self->{id
}, 'subject', encode
('UTF-8', $self->{subject
}));
180 $ruledb->{dbh
}->do("INSERT INTO Attribut " .
181 "(Object_ID, Name, Value) " .
182 "VALUES (?, ?, ?)", undef,
183 $self->{id
}, 'body', encode
('UTF-8', $self->{body
}));
184 $ruledb->{dbh
}->do("INSERT INTO Attribut " .
185 "(Object_ID, Name, Value) " .
186 "VALUES (?, ?, ?)", undef,
187 $self->{id
}, 'attach', $self->{attach
});
189 $ruledb->{dbh
}->commit if !$no_trans;
192 die $err if !$no_trans;
193 $ruledb->{dbh
}->rollback;
203 my ($self, $queue, $ruledb, $mod_group, $targets,
204 $msginfo, $vars, $marks) = @_;
208 my $from = 'postmaster';
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);
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
});
224 my $top = MIME
::Entity-
>build(
230 if ($self->{attach
} eq 'O') {
231 # attach original mail
232 my $spooldir = $PMG::MailQueue
::spooldir
;
233 my $path = "$spooldir/active/$queue->{uid}";
234 $original = $top->attach(
236 Filename
=> "original_message.eml",
237 Type
=> "message/rfc822",);
240 if ($msginfo->{testmode
}) {
241 my $fh = $msginfo->{test_fh
};
242 print $fh "notify: $self->{to}\n";
243 print $fh "notify content:\n";
245 if ($self->{attach
} eq 'O') {
246 # make result reproducable for regression testing
247 $top->head->replace('content-type',
248 'multipart/mixed; boundary="---=_1234567"');
251 print $fh "notify end\n";
253 my @targets = split(/\s*,\s*/, $to);
254 my $qid = PMG
::Utils
::reinject_mail
(
255 $top, $from, \
@targets, undef, $msginfo->{fqdn
});
258 syslog
('info', "%s: notify <%s> (%s)", $queue->{logid
}, $_, $qid);
260 syslog
('err', "%s: notify <%s> failed", $queue->{logid
}, $_);
269 return "notify $self->{to}";
277 description
=> "The Receiver E-Mail address",
282 description
=> "The Notification subject",
287 description
=> "Attach original E-Mail",
293 description
=> "The Notification Body",
305 subject
=> $self->{subject
},
306 body
=> $self->{body
},
307 attach
=> ($self->{attach
} eq 'O') ?
1 : 0,
312 my ($self, $param) = @_;
314 $self->{to
} = $param->{to
};
315 $self->{subject
} = $param->{subject
};
316 $self->{body
} = $param->{body
};
317 $self->{attach
} = $param->{attach
} ?
'O' : 'N';
324 =head1 PMG::RuleDB::Notify