use strict;
use File::Spec;
my $PREFIX = '@@PREFIX@@'; my $DEF_RULES_DIR = '@@DEF_RULES_DIR@@'; my $LOCAL_RULES_DIR = '@@LOCAL_RULES_DIR@@';
use lib '@@INSTALLSITELIB@@';
BEGIN {
my @bin = File::Spec->splitpath($0);
my $bin = (
$bin[0]
? File::Spec->catpath( @bin[ 0 .. 1 ] )
: $bin[1]
) || File::Spec->curdir;
if ( -e $bin . '/lib/Mail/SpamAssassin.pm'
|| !-e '@@INSTALLSITELIB@@/Mail/SpamAssassin.pm' )
{
if ( $bin eq '../' && -e '../blib/lib/Mail/SpamAssassin.pm' ) {
unshift ( @INC, '../blib/lib' );
}
else {
foreach (
qw(lib ../lib/site_perl
../lib/spamassassin ../share/spamassassin/lib)
)
{
my $dir = File::Spec->catdir( $bin, split ( '/', $_ ) );
if ( -f File::Spec->catfile( $dir, "Mail", "SpamAssassin.pm" ) ) {
unshift ( @INC, $dir );
last;
}
}
}
}
}
use Getopt::Long;
use Pod::Usage;
use Mail::SpamAssassin;
use Mail::SpamAssassin::ArchiveIterator;
my %resphash = (
EX_OK => 0, EX_USAGE => 64, EX_DATAERR => 65, EX_NOINPUT => 66, EX_NOUSER => 67, EX_NOHOST => 68, EX_UNAVAILABLE => 69, EX_SOFTWARE => 70, EX_OSERR => 71, EX_OSFILE => 72, EX_CANTCREAT => 73, EX_IOERR => 74, EX_TEMPFAIL => 75, EX_PROTOCOL => 76, EX_NOPERM => 77, EX_CONFIG => 78, );
sub print_version {
print "SpamAssassin version " . Mail::SpamAssassin::Version() . "\n"
. " running on Perl version " . join(".", map { $_ * 1 } ($] =~ /(\d)\.(\d{3})(\d{3})/ )) . "\n";
}
sub print_usage_and_exit {
my ( $message, $respnam ) = @_;
$respnam ||= 'EX_USAGE';
if ($respnam eq 'EX_OK' ) {
print_version();
print("\n");
}
pod2usage(
-verbose => 0,
-message => $message,
-exitval => $resphash{$respnam},
);
}
sub usage {
my ( $verbose, $message ) = @_;
my $ver = Mail::SpamAssassin::Version();
print "SpamAssassin version $ver\n";
pod2usage( -verbose => $verbose, -message => $message, -exitval => 64 );
}
if ($Mail::SpamAssassin::VERSION ne '@@VERSION@@' && '@@VERSION@@' ne "\@\@VERSION\@\@") {
die 'ERROR! spamassassin script is v@@VERSION@@, but using modules v'.$Mail::SpamAssassin::VERSION."!\n";
}
my %opt = ( 'create-prefs' => 1, 'format' => 'detect' );
my $doing_whitelist_operation = 0;
my $count = 0;
my @targets = ();
my $exitvalue;
Getopt::Long::Configure(
qw(bundling no_getopt_compat no_auto_abbrev no_ignore_case));
GetOptions(
'add-addr-to-blacklist=s' => \$opt{'add-addr-to-blacklist'},
'add-addr-to-whitelist=s' => \$opt{'add-addr-to-whitelist'},
'add-to-blacklist' => \$opt{'add-to-blacklist'},
'add-to-whitelist|W' => \$opt{'add-to-whitelist'},
'configpath|config-file|config-dir|c|C=s' => \$opt{'configpath'},
'create-prefs!' => \$opt{'create-prefs'},
'debug-level|debug|D:s' => \$opt{'debug-level'},
'error-code|exit-code|e:i' => \$opt{'error-code'},
'help|h|?' => \$opt{'help'},
'lint' => \$opt{'lint'},
'local-only|local|L' => \$opt{'local'},
'mbox' => sub { $opt{'format'} = 'mbox'; },
'mbx' => sub { $opt{'format'} = 'mbx'; },
'prefspath|prefs-file|p=s' => \$opt{'prefspath'},
'remove-addr-from-whitelist=s' => \$opt{'remove-addr-from-whitelist'},
'remove-from-whitelist|R' => \$opt{'remove-from-whitelist'},
'remove-markup|despamassassinify|d' => \$opt{'remove-markup'},
'report|r' => \$opt{'report'},
'revoke|k' => \$opt{'revoke'},
'siteconfigpath=s' => \$opt{'siteconfigpath'},
'test-mode|test|t' => \$opt{'test-mode'},
'version|V' => \$opt{'version'},
'x' => sub { $opt{'create-prefs'} = 0 },
'pipe|P' => sub { warn "The -P option is deprecated as 'pipe mode' is now the default behavior, ignoring.\n" },
'F:i' => sub { warn "The -F option has been removed from spamassassin, please remove from your commandline and re-run.\n"; exit 2; },
'add-from!' => sub { warn "The --add-from option has been removed from spamassassin, please remove from your commandline and re-run.\n"; exit 2; },
'stop-at-threshold|S' => sub { warn "The -S option has been deprecated and is no longer supported, ignoring.\n" },
'log-to-mbox|l:s' => sub { warn "The -l option has been deprecated and is no longer supported, ignoring.\n" },
'auto-whitelist|a' => sub { warn "The -a option has been removed. Please look at the use_auto_whitelist config option instead.\n"; exit 2; },
'warning-from|w:s' => sub { warn "The -w option has been removed from spamassassin, please remove from your commandline and re-run.\n"; exit 2; },
'whitelist-factory|M:s' => sub { warn "The -M option has been removed from spamassassin, please remove from your commandline and re-run.\n"; exit 2; },
) or print_usage_and_exit();
if ( defined $opt{'help'} ) {
print_usage_and_exit("For more information read the spamassassin man page.\n", 'EX_OK');
}
if ( defined $opt{'version'} ) {
print_version();
exit($resphash{'EX_OK'});
}
my $spamtest = new Mail::SpamAssassin(
{
rules_filename => $opt{'configpath'},
site_rules_filename => $opt{'siteconfigpath'},
userprefs_filename => $opt{'prefspath'},
local_tests_only => $opt{'local'},
debug => defined( $opt{'debug-level'} ),
dont_copy_prefs => ( $opt{'create-prefs'} ? 0 : 1 ),
PREFIX => $PREFIX,
DEF_RULES_DIR => $DEF_RULES_DIR,
LOCAL_RULES_DIR => $LOCAL_RULES_DIR,
}
);
if ( defined $opt{'debug-level'} && $opt{'debug-level'} ne '' ) {
my $levels = $opt{'debug-level'};
while ( $levels =~ s/^([a-z]+)=([+-]?\d+)[,;:]*//s ) {
$Mail::SpamAssassin::DEBUG->{$1} = $2 + 0;
}
if ( $levels !~ /^\s*$/ ) {
print_usage_and_exit("bad areas in --debug option ($levels)!");
}
}
if ( $opt{'lint'} ) {
$spamtest->debug_diagnostics();
my $res = $spamtest->lint_rules();
warn "lint: $res issues detected. please rerun with debug enabled for more information.\n" if ($res);
exit $res ? 1: 0;
}
if ( $opt{'remove-addr-from-whitelist'}
or $opt{'add-addr-to-whitelist'}
or $opt{'add-addr-to-blacklist'} )
{
$spamtest->init(1);
if ( $opt{'add-addr-to-whitelist'} ) {
$spamtest->add_address_to_whitelist( $opt{'add-addr-to-whitelist'} );
}
elsif ( $opt{'remove-addr-from-whitelist'} ) {
$spamtest->remove_address_from_whitelist(
$opt{'remove-addr-from-whitelist'} );
}
elsif ( $opt{'add-addr-to-blacklist'} ) {
$spamtest->add_address_to_blacklist( $opt{'add-addr-to-blacklist'} );
}
else {
die "oops! unhandled whitelist operation";
}
exit(0);
}
if ( $opt{'remove-from-whitelist'}
or $opt{'add-to-whitelist'}
or $opt{'add-to-blacklist'} )
{
$doing_whitelist_operation = 1;
$spamtest->init(1);
}
if ($opt{'test-mode'}) {
$spamtest->{'conf'}->{'use_auto_whitelist'} = 0;
$spamtest->{'conf'}->{'bayes_auto_learn'} = 0;
}
my $iter = new Mail::SpamAssassin::ArchiveIterator(
{
'opt_j' => 0,
'opt_n' => 1,
'opt_all' => 1,
}
);
$iter->set_functions( \&wanted, sub { } );
push ( @ARGV, '-' ) if ( !@ARGV );
@targets = map { join ( ":", '', $opt{'format'}, $_ ) } @ARGV;
my $tempfile;
if ( $targets[0] =~ /:mbo?x:-$/ ) {
my $handle;
local $/ = undef; ( $tempfile, $handle ) = Mail::SpamAssassin::Util::secure_tmpfile();
print {$handle} <STDIN>;
close $handle;
$targets[0] =~ s/:-$/:$tempfile/;
}
eval { $iter->run(@targets); };
if ( defined $tempfile ) {
unlink $tempfile;
}
if ( $opt{'report'} || $opt{'revoke'} || $doing_whitelist_operation ) {
print "$count message(s) examined.\n";
}
if ($@) { die $@; }
exit( $exitvalue || 0 );
sub NEVERCALLED {
$Mail::SpamAssassin::DEBUG = {};
}
sub wanted {
my $dataref = $_[3];
my $mail = $spamtest->parse($dataref);
$count++;
if ($doing_whitelist_operation) {
if ( $opt{'add-to-whitelist'} ) {
$spamtest->add_all_addresses_to_whitelist($mail);
}
elsif ( $opt{'remove-from-whitelist'} ) {
$spamtest->remove_all_addresses_from_whitelist($mail);
}
elsif ( $opt{'add-to-blacklist'} ) {
$spamtest->add_all_addresses_to_blacklist($mail);
}
else {
warn "oops! unhandled whitelist operation";
}
$mail->finish();
return;
}
if ( $opt{'remove-markup'} ) {
if ( !$opt{'test-mode'} ) {
print $spamtest->remove_spamassassin_markup ($mail);
$mail->finish();
return;
}
else {
my $new_mail =
$spamtest->parse( $spamtest->remove_spamassassin_markup($mail) );
$mail->finish();
$mail = $new_mail;
}
}
if ( $opt{'report'} || $opt{'revoke'} ) {
my $new_mail =
$spamtest->parse( $spamtest->remove_spamassassin_markup($mail) );
$mail->finish();
my $failed;
if ( $opt{'report'} && $spamtest->report_as_spam($new_mail) ) {
$failed = 'report';
}
if ( $opt{'revoke'} && $spamtest->revoke_as_spam($new_mail) ) {
$failed = 'revoke';
}
if ($failed) {
warn "Warning, unable to $failed message\n";
warn "For more information, re-run with -D option to see debug output.\n";
}
$new_mail->finish();
return;
}
my $status = $spamtest->check($mail);
print $status->rewrite_mail ();
if ( $opt{'test-mode'} ) {
print $status->get_report();
}
if ( defined $opt{'error-code'} && $status->is_spam() && !defined $exitvalue )
{
$exitvalue = $opt{'error-code'} || 5;
}
$mail->finish();
$status->finish();
return;
}
=head1 NAME
spamassassin - mail filter to identify spam using text analysis
=head1 SYNOPSIS
B<spamassassin> [options] [ < I<mailmessage> | I<path> ... ]
B<spamassassin> B<-d> [ < I<mailmessage> | I<path> ... ]
B<spamassassin> B<-r> [B<-w> I<addr>] [ < I<mailmessage> | I<path> ... ]
B<spamassassin> B<-k> [B<-w> I<addr>] [ < I<mailmessage> | I<path> ... ]
B<spamassassin> B<-W>|B<-R> [ < I<mailmessage> | I<path> ... ]
Options:
-L, --local Local tests only (no online tests)
-r, --report Report message as spam
-k, --revoke Revoke message as spam
-d, --remove-markup Remove spam reports from a message
-C path, --configpath=path, --config-file=path
Path to standard configuration dir
-p prefs, --prefspath=file, --prefs-file=file
Set user preferences file
--siteconfigpath=path Path for site configs
(def: /etc/mail/spamassassin)
-x, --nocreate-prefs Don't create user preferences file
-e, --exit-code Exit with a non-zero exit code if the
tested message was spam
--mbox read in messages in mbox format
--mbx read in messages in UW mbx format
-t, --test-mode Pipe message through and add extra
report to the bottom
--lint Lint the rule set: report syntax errors
-W, --add-to-whitelist Add addresses in mail to whitelist (AWL)
--add-to-blacklist Add addresses in mail to blacklist (AWL)
-R, --remove-from-whitelist Remove all addresses found in mail
from whitelist (AWL)
--add-addr-to-whitelist=addr Add addr to whitelist (AWL)
--add-addr-to-blacklist=addr Add addr to blacklist (AWL)
--remove-addr-from-whitelist=addr Remove addr from whitelist (AWL)
-D, --debug [area=n,...] Print debugging messages
-V, --version Print version
-h, --help Print usage message
=head1 DESCRIPTION
SpamAssassin is a mail filter to identify spam using text analysis and several
internet-based realtime blacklists.
Using its rule base, it uses a wide range of heuristic tests on mail headers
and body text to identify "spam", also known as unsolicited commercial email.
Once identified, the mail is then tagged as spam for later filtering using the
user's own mail user-agent application.
SpamAssassin also includes support for reporting spam messages to collaborative
filtering databases, such as Vipul's Razor ( http://razor.sourceforge.net/ ).
The default tagging operations that take place are detailed in L</TAGGING>.
By default, message(s) are read in from STDIN (< I<mailmessage>), or
from specified files and directories (I<path> ...) STDIN and files
are assumed to be in I<file> format, with a single message per file.
Directories are assumed to be in a format where each file in the directory
contains only one message (directories are not recursed and filenames
containing whitespace or beginning with "." or "," are skipped).
The options I<--mbox> and I<--mbx> can override the assumed format,
see the appropriate OPTION information below.
=head1 OPTIONS
=over 4
=item B<-e>, B<--error-code>, B<--exit-code>
Exit with a non-zero error code, if the message is determined to be
spam.
=item B<-h>, B<--help>
Print help message and exit.
=item B<-t>, B<--test-mode>
Test mode. Pipe message through and add extra report. Note that the report
text assumes that the message is spam, since in normal use it is only visible
in this case. Pay attention to the score instead.
If you run this with B<-d>, the message will first have SpamAssassin
markup removed before being tested.
If you run tests with the auto-whitelist enabled, the score result will be
added to the AWL. This may not be what you want to do. If it is not,
then disable the auto-whitelist.
=item B<-r>, B<--report>
Report this message as manually-verified spam. This will submit the mail
message read from STDIN to various spam-blocker databases. Currently,
these are the Distributed Checksum Clearinghouse
C<http://www.rhyolite.com/anti-spam/dcc/>, Pyzor
C<http://pyzor.sourceforge.net/>, Vipul's Razor
C<http://razor.sourceforge.net/>, and SpamCop C<http://www.spamcop.net/>.
If the message contains SpamAssassin markup, the markup will be stripped
out automatically before submission. The support modules for DCC, Pyzor,
and Razor must be installed for spam to be reported to each service.
SpamCop reports will have greater effect if you register and set the
C<spamcop_submission_address> option.
The message will also be submitted to SpamAssassin's learning systems;
currently this is the internal Bayesian statistical-filtering system (the
BAYES rules). (Note that if you I<only> want to perform statistical
learning, and do not want to report mail to third-parties, you should use
the C<sa-learn> command directly instead.)
=item B<-k>, B<--revoke>
Revoke this message. This will revoke the mail message read from STDIN from
various spam-blocker databases. Currently, these are Vipul's Razor.
Revocation support for the Distributed Checksum Clearinghouse, Pyzor, and
SpamCop is not currently available.
If the message contains SpamAssassin markup, the markup will be stripped
out automatically before submission. The support modules for Razor must
be installed for spam to be revoked from the service.
The message will also be submitted as 'ham' (non-spam) to SpamAssassin's
learning systems; currently this is the internal Bayesian
statistical-filtering system (the BAYES rules). (Note that if you I<only>
want to perform statistical learning, and do not want to report mail to
third-parties, you should use the C<sa-learn> command directly instead.)
=item B<--lint>
Syntax check (lint) the rule set and configuration files, reporting
typos and rules that do not compile correctly. Exits immediately with
0 if there are no errors, or greater than 0 if any errors are found.
=item B<-W>, B<--add-to-whitelist>
Add all email addresses, in the headers and body of the mail message read
from STDIN, to the automatic whitelist. Note that you must be running
C<spamassassin> or C<spamd> with the auto-whitelist enabled for this
to work.
=item B<--add-to-blacklist>
Add all email addresses, in the headers and body of the mail message read
from STDIN, to the automatic whitelist with a high score (ensuring they
will be ''blacklisted''). Note that you must be running C<spamassassin>
or C<spamd> with the auto-whitelist enabled.
=item B<-R>, B<--remove-from-whitelist>
Remove all email addresses, in the headers and body of the mail message read
from STDIN, from the automatic whitelist. STDIN must contain a full email
message, so to remove a single address you should use
B<--remove-addr-from-whitelist> instead.
Note that you must be running C<spamassassin> or C<spamd> with the
auto-whitelist enabled.
=item B<--add-addr-to-whitelist>
Add the named email address to the automatic whitelist. Note that you
must be running C<spamassassin> or C<spamd> with the auto-whitelist
enabled.
=item B<--add-addr-to-blacklist>
Add the named email address to the automatic whitelist with a high score
(ensuring they will be ''blacklisted''). Note that you must be running
C<spamassassin> or C<spamd> with the auto-whitelist enabled.
=item B<--remove-addr-from-whitelist>
Remove the named email address from the automatic whitelist. Note that
you must be running C<spamassassin> or C<spamd> with the auto-whitelist
enabled.
=item B<-L>, B<--local>
Do only the ''local'' tests, ones that do not require an internet connection to
operate. Normally, SpamAssassin will try to detect whether you are connected
to the net before doing these tests anyway, but for faster checks you may wish
to use this.
Note that SpamAssassin's network rules are run in parallel. This can cause
overhead in terms of the number of file descriptors required if B<--local> is
not used; it is recommended that the minimum limit on fds be raised to at least
256 for safety.
=item B<-d>, B<--remove-markup>
Remove SpamAssassin markup (the "SpamAssassin results" report, X-Spam-Status
headers, etc.) from the mail message. The resulting message, which will be
more or less identical to the original, pre-SpamAssassin input, will be output
to STDOUT.
(Note: the message will not be exactly identical; some headers will be
reformatted due to some features of the Mail::Internet package, but the body
text will be.)
=item B<-C> I<path>, B<--configpath>=I<path>, B<--config-file>=I<path>
Use the specified path for locating the distributed configuration files.
Ignore the default directories (usually C</usr/share/spamassassin> or similar).
=item B<--siteconfigpath>=I<path>
Use the specified path for locating site-specific configuration files. Ignore
the default directories (usually C</etc/mail/spamassassin> or similar).
=item B<-p> I<prefs>, B<--prefspath>=I<prefs>, B<--prefs-file>=I<prefs>
Read user score preferences from I<prefs> (usually C<$HOME/.spamassassin/user_prefs>).
=item B<-D> [I<area=n,...>], B<--debug> [I<area=n,...>]
Produce diagnostic output. The level of diagnostic output can be set for each
area separately; I<area> is the area of the code to instrument, and I<n> is
a positive or negative number indicating the debug level or bitmask for that
area of code. For example, to produce diagnostic output on all rules that
hit, use:
spamassassin -D rulesrun=255
=item B<-x>, B<--nocreate-prefs>
Disable creation of user preferences file.
=item B<--mbox>
Specify that the input message(s) are in mbox format. mbox is a standard
Unix message folder format.
=item B<--mbx>
Specify that the input message(s) are in UW .mbx format. mbx is
the mailbox format used within the University of Washington's IMAP
implementation; see C<http://www.washington.edu/imap/>.
=back
=head1 CONFIGURATION FILES
The rule base, text templates, and rule description text are loaded from the
configuration files.
By default, configuration data is loaded from the first existing directory in:
F<@@DEF_RULES_DIR@@>;
F<@@PREFIX@@/usr/share/spamassassin>;
F</usr/local/share/spamassassin>;
F</usr/share/spamassassin> .
Site-specific configuration data is used to override any values which had
already been set. This is loaded from the first existing directory in:
F<@@LOCAL_RULES_DIR@@>;
F<@@PREFIX@@/etc/mail/spamassassin>;
F<@@PREFIX@@/etc/spamassassin>;
F</usr/local/etc/spamassassin>;
F</usr/pkg/etc/spamassassin>;
F</usr/etc/spamassassin>;
F</etc/mail/spamassassin>;
F</etc/spamassassin> .
Spamassassin will read *.cf in these directories, in alphanumeric order within
each directory (similar to SysV-style startup scripts). In other words, it
will read F<10_misc.cf> before F<50_scores.cf> and F<20_body_tests.cf> before
F<20_head_test.cf>. Options in later files will override earlier files.
The user preferences (such as scores to attach to each rule), are loaded from
the file specified in the B<-p> argument. If this is not specified,
F<~/.spamassassin/user_prefs> is used if it exists. C<spamassassin> will
create this file if it does not exist, using F<user_prefs.template> as a
template. This file will be looked for in:
F<@@LOCAL_RULES_DIR@@>;
F<@@PREFIX@@/etc/mail/spamassassin>;
F<@@PREFIX@@/usr/share/spamassassin>;
F</etc/spamassassin>;
F</etc/mail/spamassassin>;
F</usr/local/share/spamassassin>;
F</usr/share/spamassassin>.
=head1 TAGGING
The following two sections detail the tagging that takes place for
messages.
Note that if you use the B<-t> argument, all mails will be tagged
as if they are spam messages.
=head2 TAGGING FOR SPAM MAILS
If an incoming message is tagged as spam, instead of modifying the
original message, SpamAssassin will create a new report message and
attach the original message as a message/rfc822 MIME part (ensuring
the original message is completely preserved and easier to recover).
The new report message inherits the following headers (if they are
present) from the original spam message:
=over 4
=item Subject: header
=item From: header
=item To: header
The above headers can be modified if the relavent C<rewrite_header>
option is given.
=item Cc: header
=item Date: header
=back
And (by default) these headers are added:
=over 4
=item X-Spam-Status: header
A string, C<Yes, score=nn required=nn tests=xxx,xxx
autolearn=(ham|spam|no|unavailable|failed)> is set in this header to
reflect the filter status.
=item X-Spam-Flag: header
Set to C<YES>.
=item X-Spam-Report: header
=back
Please note that the headers that added are now fully configurable via
the add_header option. Please see the manpage for
Mail::SpamAssassin::Conf(3) for more information.
=over 4
=item spam mail body text
The SpamAssassin report is added to top of the mail message body,
if the message is marked as spam.
=back
=head2 DEFAULT TAGGING FOR HAM (NON-SPAM) MAILS
=over 4
=item X-Spam-Status: header
A string, C<No, score=nn required=nn tests=xxx,xxx
autolearn=(ham|spam|no|unavailable|failed)> is set in this header to
reflect the filter status.
=back
Added headers are fully configurable via the add_header configuration
option. Please see the manpage for Mail::SpamAssassin::Conf(3) for
more information.
=head1 INSTALLATION
The B<spamassassin> command is part of the B<Mail::SpamAssassin> Perl module.
Install this as a normal Perl module, using C<perl -MCPAN -e shell>, or by
hand.
For further details on how to install, please read the C<INSTALL> file
from the SpamAssassin distribution.
=head1 SEE ALSO
sa-learn(1)
spamd(1)
spamc(1)
Mail::SpamAssassin::Conf(3)
Mail::SpamAssassin(3)
=head1 PREREQUISITES
C<Mail::SpamAssassin>
=head1 BUGS
See <http://bugzilla.spamassassin.org/>
=head1 AUTHORS
The SpamAssassin(tm) Project <http://spamassassin.apache.org/>
=head1 COPYRIGHT
SpamAssassin is distributed under the Apache License, Version 2.0, as
described in the file C<LICENSE> included with the distribution.
=cut