<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><title>courierperlfilter</title><link rel="stylesheet" type="text/css" href="style.css"/><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"/><link rel="home" href="#courierperlfilter" title="courierperlfilter"/><link xmlns="" rel="stylesheet" type="text/css" href="manpage.css"/><meta xmlns="" name="MSSmartTagsPreventParsing" content="TRUE"/><link xmlns="" rel="icon" href="icon.gif" type="image/gif"/><!--

Copyright 1998 - 2009 S. Varshavchik.  See COPYING for distribution
information.

--></head><body><div class="refentry"><a id="courierperlfilter" shape="rect"> </a><div class="titlepage"/><div class="refnamediv"><h2>Name</h2><p>courierperlfilter — Sample Perl-based mail filter</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">filterctl</code>  [[start] |  [stop]] [perlfilter]</p></div></div><div class="refsect1"><a id="courierperlfilter_description" shape="rect"> </a><h2>DESCRIPTION</h2><p>
This is an example global mail filter that uses an embedded Perl script.
"Embedded" means that the Perl interpreter is loaded once, and the same Perl
code is repeatedly called to accept or reject incoming messages, one by one.
Perl filtering is relatively time consuming (compared to filtering in C or
C++), and excessive delays in mail filters result in incoming mail being
deferred (rejected with a temporary error code).  Therefore the
<span class="command"><strong>perlfilter</strong></span> wrapper can create multiple
<span class="command"><strong>perlfilter</strong></span>
processes, so that multiple processes are used to filter incoming mail.</p><p>
<span class="command"><strong>perlfilter</strong></span> requires Perl 5.10 or higher. The best way to
create a Perl filter is to start with the sample filter,
<code class="filename">@datadir@/perlfilter-example.pl</code>. This filter reject messages that
contain an excessively long Date: header (designed to crash certain
poorly-written mail clients). Use it as a basis for writing your own filter.
You can install your filter in any convenient location, then initialize the
<code class="filename">@sysconfdir@/filters/perlfilter</code> configuration file, as described
below. Run <span class="command"><strong>filterctl start perlfilter</strong></span> to activate filtering (if
necessary, run <span class="command"><strong>courierfilter start</strong></span> to start the mail filtering
subsystem).</p><div class="refsect2"><a id="courierperlfilter_setting_up_a_perl_script" shape="rect"> </a><h3>Setting up a Perl script</h3><p>
Most of the ugly details of connecting the Perl script to Courier's mail
filtering engine is taken care of by the sample
<code class="filename">perlfilter-example.pl</code> script. One big no-no: the script MAY NOT
change the current directory. Anything else goes, for the most part.  Loading
other modules and classes, pretty much anything else you can do with Perl, is
allowed.</p><p>
The Perl script, just like any other mail filtering module, receives a
pointer to a data file and one or more control files, each time a message is
submitted to Courier for delivery. The sample script calls the
<code class="function">filterdata</code>() function to process the data file. The data file
contains the actual message. The <code class="function">filtercontrol</code>() function is
called to process each control file.  The control file contains recipient and
message metadata.  There may be more than one control file for each message.
The example script includes an implementation of <code class="function">filterdata</code>()
that blocks messages with corrupted headers. The example script doesn't do
anything interesting with <code class="function">filtercontrol</code>().</p><p>
<code class="function">filterdata</code>() and <code class="function">filtercontrol</code>() must return an
empty string if no serious objections are raised for this message. Any other
return string is interpreted as an SMTP-style error code that is used to
reject the message.  Care must be taken that any error messages are formatted
strictly according to the format of SMTP error messages (even though the
message may not actually come in via SMTP).</p></div></div><div class="refsect1"><a id="courierperlfilter_credits" shape="rect"> </a><h2>CREDITS</h2><p>
A lot of the Perl glue code is based on examples from the
<code class="literal">perlembed</code> manual page, and other sources.</p></div><div class="refsect1"><a id="courierperlfilter_files" shape="rect"> </a><h2>FILES</h2><p>
<span class="command"><strong>perlfilter</strong></span> uses the following configuration files. Changes to
the following files do not take effect until the filter has been stopped and
restarted.</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="filename">@sysconfdir@/filters/perlfilter-mode</code></span></dt><dd><p>
If this file exists
and contains the word "all", <span class="command"><strong>perlfilter</strong></span> will create its
socket in <code class="filename">@localstatedir@/allfilters</code>, otherwise the socket
will be created in <code class="filename">@localstatedir@/filters</code>, see
<a class="ulink" href="courierfilter.html" target="_top" shape="rect"><span class="citerefentry"><span class="refentrytitle">courierfilter</span>(8)</span></a>
for more information.</p></dd><dt><span class="term"><code class="filename">@sysconfdir@/filters/perlfilter-numprocs</code></span></dt><dd><p>
This file contains a number that sets how many <span class="command"><strong>perlfilter</strong></span>
processes are
created. The default is 5 processes. There's always an extra
<span class="command"><strong>perlfilter</strong></span> process that's used to clean up crashed child
processes.</p></dd><dt><span class="term"><code class="filename">@sysconfdir@/filters/perlfilter</code></span></dt><dd><p>
This file MUST exist and
it must contain a single line of text with the filename of the Perl script
to load.</p></dd><dt><span class="term"><code class="filename">@datadir@/perlfilter-example.pl</code></span></dt><dd><p>
	    This is a sample Perl
	    script of the kind that
	    <code class="filename">@sysconfdir@/filters/perlfilter</code>
	    points to.  Use it as an example of writing your own Perl
	    filters.</p></dd><dt><span class="term"><code class="filename">@datadir@/perlfilter-ratelimit.pl</code></span></dt><dd><p>
	    This is a complete Perl-based filter than implements basic
	    rate-limiting
	    features.</p></dd></dl></div><p>
Please exercise good judgment in writing Perl-based filters.  They should
be reasonably fast, and do not allocate megabytes of memory.  They should not
be very promiscuous in creating global Perl variables, and should clean up
after themselves.  The current Perl wrapper does not destroy the Perl symbol
table after each call to the filter script.  However, do not take that for
granted. This may change in the future.</p></div><div class="refsect1"><a id="courierperlfilter_see_also" shape="rect"> </a><h2>SEE ALSO</h2><p>
<a class="ulink" href="courierfilter.html" target="_top" shape="rect"><span class="citerefentry"><span class="refentrytitle">courierfilter</span>(8)</span></a>.</p></div></div></body></html>
