[Mimedefang] Search and replace placeholders with MIMEDefang
Steffen Kaiser
skmimedefang at smail.inf.fh-bonn-rhein-sieg.de
Wed Mar 11 04:01:40 EDT 2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Fri, 6 Mar 2009, Martin Zapfl wrote:
> I made some trials but unfortuately i did not get it working.
> Maybe you could send me a short example code how to replace texta with textb
> by using action_replace_with_warning():
I didn't mean to _use_ the function, but model your function in its way.
But there is even a better template function to look at:
append_to_html_part() or action_external_filter() in mimedefang.pl
e.g.: in sub filter(), you get the MIME::Entity object as 1st argument,
say "$entity".
my $path = $entry->bodyhandle;
$path = $path->path if defined $path;
if(defined($path) && open(IN, '<', $path)) {
my $changed;
if(open(OUT, '>', $path . '.tmp')) {
while(<IN>) {
s/texta/textb/ and $changed = 1;
print OUT;
}
close OUT
or $changed = undef; # error
}
close IN
or $changed = undef; # error
if(defined $changed) {
unlink($path.'.old');
if(rename($path, $path.'.old')) { # backup
if(rename($path.'.tmp', $path)) {
# OK, changed content in place
$Changed = 1;
unlink($path.'.old');
} else {
rename($path.'.old', $path); # rollback
}
}
}
unlink($path.'.tmp');
}
this snippet first tries to get a real file handle for the part, then read
it line-by-line and replaces the text. If anything was changed, but no
error happend, the file is replaced by the temporary OUT file. $Changed
signals to the mimedefang core, that the body had been changed.
Again, be sure to mock with text parts only.
Bye,
- --
Steffen Kaiser
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iQEVAwUBSbdv59lJzF6z/k3SAQKBTwgAo8hWq1plVPhtdKbS5XwNp1qiFZLB6aOR
6X1JsXv32Q9jMydmPspMF3Jwe83M/S4O4etwaMj2bhlJdZumt1lo0R6QLyUc0Eo+
b4EUw7Zv+q3d/PlYrceXrMgg72Tqi7iDruvB7/1mxp9xYaPBYSQTDpiWGMMj4KCR
Q6U5c4htnM1/g9HW7CLl2mqMgOaXTdH1Jx5IWuMvRgbE6CjXV8fPHNwPz8z0Sp6I
/UQMbX0M4Tdj9HUUJdAN+O+vs5TpC3H5yzmyM+uYmyDQHogoSWqE6yFE/ySPf3l+
FpXYzaQJML+YqngmbuLrn1hKyEOIZkB0YiNkg2Ut9Mm5cmAI7eTOrQ==
=PEQh
-----END PGP SIGNATURE-----
More information about the MIMEDefang
mailing list