]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/RuleDB/Notify.pm
1 package PMG
::RuleDB
::Notify
;
9 use MIME
::Words
qw(encode_mimewords);
10 use Encode
qw(decode encode);
16 use PMG
::RuleDB
::Object
;
19 use base
qw(PMG::RuleDB::Object);
30 return 'Notification';
42 my ($type, $to, $subject, $body, $attach, $ogroup) = @_;
44 my $class = ref($type) || $type;
46 my $self = $class->SUPER::new
($class->otype(), $ogroup);
50 $subject //= 'Notification: __SUBJECT__';
52 if (!defined($body)) {
57 Receiver: __RECEIVERS__
62 Matching Rule: __RULE__
71 $self->{subject
} = $subject;
72 $self->{body
} = $body;
73 $self->{attach
} = $attach;
79 my ($type, $ruledb, $id, $ogroup, $value) = @_;
81 my $class = ref($type) || $type;
83 defined($value) || die "undefined object attribute: ERROR";
85 my ($raw_subject, $raw_body, $attach);
87 my $sth = $ruledb->{dbh
}->prepare(
88 "SELECT * FROM Attribut WHERE Object_ID = ?");
92 while (my $ref = $sth->fetchrow_hashref()) {
93 $raw_subject = $ref->{value
} if $ref->{name
} eq 'subject';
94 $raw_body = $ref->{value
} if $ref->{name
} eq 'body';
95 $attach = $ref->{value
} if $ref->{name
} eq 'attach';
100 # Note: db stores binary (ascii) data, so we need to convert to UTF-8 manually
101 my $obj = $class->new($value,
102 decode
('UTF-8', $raw_subject),
103 decode
('UTF-8', $raw_body),
107 # Note: Digest::SHA does not work with wide UTF8 characters
108 $obj->{digest
} = Digest
::SHA
::sha1_hex
(
109 $id, $obj->{to
}, $raw_subject, $raw_body, $obj->{attach
}, $ogroup);
115 my ($self, $ruledb, $no_trans) = @_;
117 defined($self->{ogroup
}) || die "undefined object attribute: ERROR";
118 defined($self->{to
}) || die "undefined object attribute: ERROR";
119 defined($self->{subject
}) || die "undefined object attribute: ERROR";
120 defined($self->{body
}) || die "undefined object attribute: ERROR";
122 $self->{attach
} //= 'N';
124 # Note: db stores binary (ascii) data, so we need to convert from UTF-8 manually
126 if (defined ($self->{id
})) {
130 $ruledb->{dbh
}->begin_work if !$no_trans;
133 "UPDATE Object SET Value = ? WHERE ID = ?",
134 undef, $self->{to
}, $self->{id
});
137 "UPDATE Attribut SET Value = ? " .
138 "WHERE Name = ? and Object_ID = ?",
139 undef, encode
('UTF-8', $self->{subject
}), 'subject', $self->{id
});
142 "UPDATE Attribut SET Value = ? " .
143 "WHERE Name = ? and Object_ID = ?",
144 undef, encode
('UTF-8', $self->{body
}), 'body', $self->{id
});
147 "UPDATE Attribut SET Value = ? " .
148 "WHERE Name = ? and Object_ID = ?",
149 undef, $self->{attach
}, 'attach', $self->{id
});
151 $ruledb->{dbh
}->commit if !$no_trans;
154 die $err if !$no_trans;
155 $ruledb->{dbh
}->rollback;
163 $ruledb->{dbh
}->begin_work if !$no_trans;
167 my $sth = $ruledb->{dbh
}->prepare(
168 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
169 "VALUES (?, ?, ?);");
171 $sth->execute($self->ogroup, $self->otype, $self->{to
});
173 $self->{id
} = PMG
::Utils
::lastid
($ruledb->{dbh
}, 'object_id_seq');
177 $ruledb->{dbh
}->do("INSERT INTO Attribut " .
178 "(Object_ID, Name, Value) " .
179 "VALUES (?, ?, ?)", undef,
180 $self->{id
}, 'subject', encode
('UTF-8', $self->{subject
}));
181 $ruledb->{dbh
}->do("INSERT INTO Attribut " .
182 "(Object_ID, Name, Value) " .
183 "VALUES (?, ?, ?)", undef,
184 $self->{id
}, 'body', encode
('UTF-8', $self->{body
}));
185 $ruledb->{dbh
}->do("INSERT INTO Attribut " .
186 "(Object_ID, Name, Value) " .
187 "VALUES (?, ?, ?)", undef,
188 $self->{id
}, 'attach', $self->{attach
});
190 $ruledb->{dbh
}->commit if !$no_trans;
193 die $err if !$no_trans;
194 $ruledb->{dbh
}->rollback;
204 my ($self, $queue, $ruledb, $mod_group, $targets,
205 $msginfo, $vars, $marks) = @_;
209 my $from = 'postmaster';
211 my $rulename = $vars->{RULE
} // 'unknown';
213 my $body = PMG
::Utils
::subst_values
($self->{body
}, $vars);
214 my $subject = PMG
::Utils
::subst_values
($self->{subject
}, $vars);
215 my $to = PMG
::Utils
::subst_values
($self->{to
}, $vars);
217 if ($to =~ m/^\s*$/) {
218 # this happens if a notification is triggered by bounce mails
219 # which notifies the sender <> - we just log and then ignore it
220 syslog
('info', "%s: notify <> (rule: %s, ignored)", $queue->{logid
}, $rulename);
227 my $top = MIME
::Entity-
>build(
228 Encoding
=> 'quoted-printable',
232 Subject
=> encode_mimewords
(encode
('UTF-8', $subject), "Charset" => "UTF-8"),
233 Data
=> encode
('UTF-8', $body));
235 if ($self->{attach
} eq 'O') {
236 # attach original mail
237 my $spooldir = $PMG::MailQueue
::spooldir
;
238 my $path = "$spooldir/active/$queue->{uid}";
239 $original = $top->attach(
241 Filename
=> "original_message.eml",
242 Type
=> "message/rfc822",);
245 if ($msginfo->{testmode
}) {
246 my $fh = $msginfo->{test_fh
};
247 print $fh "notify: $self->{to}\n";
248 print $fh "notify content:\n";
250 if ($self->{attach
} eq 'O') {
251 # make result reproducible for regression testing
252 $top->head->replace('content-type',
253 'multipart/mixed; boundary="---=_1234567"');
256 print $fh "notify end\n";
258 my @targets = split(/\s*,\s*/, $to);
259 my $qid = PMG
::Utils
::reinject_mail
(
260 $top, $from, \
@targets, undef, $msginfo->{fqdn
});
263 syslog
('info', "%s: notify <%s> (rule: %s, %s)", $queue->{logid
}, $_, $rulename, $qid);
265 syslog
('err', "%s: notify <%s> (rule: %s) failed", $queue->{logid
}, $_, $rulename);
274 return "notify $self->{to}";
282 description
=> "The Receiver E-Mail address",
287 description
=> "The Notification subject",
292 description
=> "Attach original E-Mail",
298 description
=> "The Notification Body",
310 subject
=> $self->{subject
},
311 body
=> $self->{body
},
312 attach
=> ($self->{attach
} eq 'O') ?
1 : 0,
317 my ($self, $param) = @_;
319 $self->{to
} = $param->{to
};
320 $self->{subject
} = $param->{subject
};
321 $self->{body
} = $param->{body
};
322 $self->{attach
} = $param->{attach
} ?
'O' : 'N';
329 =head1 PMG::RuleDB::Notify