#!/usr/bin/perl
##
## razor-check - check spam message against a Razor Catalogue Server.
##
## Copyright (c) 1998, Vipul Ved Prakash.  All rights reserved.
## This code is free software; you can redistribute it and/or modify
## it under the same terms as Perl itself.
##
## $Id: razor-check,v 1.11 2001/12/25 09:44:30 vipul Exp $

use lib qw(lib);
use strict;
use Razor::Client;
use Razor::Agent;
use Razor::Config; 

my $options = Razor::Agent::options('razor-check');
my $config = Razor::Config::findconf ('razor.conf');
my $rc = new Razor::Agent ($config, %$options);
my @sigs = $rc->compute_sigs;
my %check = ( sigs => \@sigs );
my $hasspam;

my $response = $rc->check (%check);
unless ($response) { 
    $rc->raise_error(); 
}
if (scalar @$response == 1) { 
    if ($response->[0]) { exit 0 } else { exit 1 }
} else {
    my $counter = 1;
    for (@$response) {
        if ($_) {
            print "$counter\n";
            $hasspam = 1;
        } else { print "-$counter\n" if $$options{negative} }
        $counter++;
    }
    if ($hasspam) { exit 0 } else { exit 1 }
}
exit 1;


=head1 NAME

razor-check - Razor Filtering Agent

=head1 SYNOPSIS

    razor-check [-s -d -H] file_with_mail_in_rfc822_format

    $ cat mail | razor-check
    $ razor-check ./mail 
    $ razor-check -d ./mail

=head1 DESCRIPTION

C<razor-check> checks a mail against the distributed Razor Catalogue. It
should be invoked before the mail is delivered or processed by a human.
C<razor-check> terminates with exit value C<0> if the signature for the
mail is catalogued on the server (spam) or C<1> if the mail is not
catalogued by the server (not a spam). C<razor-check> should be invoked
against every incoming mail by mail processors (like procmail) or MTAs
(like sendmail). An alternate method would be to call C<razor-check> from
cron, at regular intervals, to identify and mark spam in queued mailboxes.

=head1 USAGE

C<razor-check> is usually run by piping the contents of the mail to it, or
by providing the name of the file that contains the mail message to be
checked as the last argument. C<razor-check> takes the following
arguments:

=over 4

=item C<-s>

Simulate a check. Do everything except talk to the server.

=item C<-d | --verbose>

Print debugging information.

=item C<-H>

Compute and print the SHA hash of the mail contents. And then exit.

=item C<-M | --mbox> 

Accept a mailbox name on the command line and check every mail in the
mailbox against the database. C<razor-check> will print out the serial
number of every mail found in the Catalogue. When combined with C<-n> it
will also print the serial numbers, prefixed with a C<->, of mails that
were not found in the Catalogue. Usage:

 razor-check -M ~/Mail/possible-spam 

=item C<-S | --sig>

Accept a list of pre-computed (with C<-H>) signatures on the command line,
instead of computing one from mail content. When called with C<--sig>,
C<razor-check> will not attempt to read from STDIN. Usage:

 razor-check -d -S a8a3d545adb73f9733675571ffeaf10cba87745b

=item C<-conf=filename>

The path of the config file. By default the path is ~/razor.conf, an
alternate path can be specified with this option. See razor.conf(5)
manpage for various configuration options.

=item C<-h>

Print a usage message and exit.

=item C<-v>

Print the version number and exit.

=back

=head1 RECIPES

C<razor-check> is usually invoked from procmail(1). Here are some common
ways of using it with procmail:

=over 4

=item To change the C<Subject> header if mail is spam:

    :0 Wc
    | razor-check
    :0 Waf
    | formail -i "Subject: Razor Warning: SPAM/UBE/UCE"

=item To add a C<X-Razor-Warning> header to spam:

    :0 Wc
    | razor-check
    :0 Waf
    | formail -A "X-Razor-Warning: SPAM."

=item To file spam in a mailbox

    :0 Wc
    | razor-check
    :0 Wa
    /home/foo/Mail/razor-caught

=back

=head1 AUTHOR

Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>

=cut

=head1 SEE ALSO

razor.conf(5), razor-report(1), razor-discover(1)

=head1 LICENSE 

This is free software, distributed under the ARTISTIC license.

