#!/usr/bin/perl -w
#
# Use Cloudmark::CMAE::Client to score the RFC822 message(s) in
# separate file(s) named on the command line.
#

use strict;
use warnings;

use blib;
use Cloudmark::CMAE::Client qw( :errors );


my $cli = Cloudmark::CMAE::Client->new(host => '127.0.0.1', port => 2703);
die "Init failed?" unless $cli;

if (my $err = $cli->get_cartridge_version(out_version => \my $version)) {
    die "Can't get cartridge version: " . CMAE_StrError($err) . "\n";
}
else {
    print "server cartridge version: $version\n";
}

if (my $err = $cli->get_max_message_size(out_size => \my $size)) {
    die "Can't get max message size: " . CMAE_StrError($err) . "\n";
}
else {
    print "max message size: ",
            $size ? sprintf("%dMB", ($size / 1000000) + 0.5) : "(none)",
            "\n";
}


undef $/;
while (<>) {
    my ($score, $e_score, $category, $sub_category, $rescan, $analysis);
    my $err;

    $err = $cli->score(rfc822 => $_, out_score => \$score,
                       out_category => \$category, out_sub_category => \$sub_category,
                       out_rescan => \$rescan, out_analysis => \$analysis);
    die "Can't score $ARGV: " . CMAE_StrError($err) . "\n"
      if $err;

    # Note that arguments other than the score are optional.
    $err = $cli->score(rfc822 => $_, envelope => { Helo => "there" }, out_score => \$e_score);
    die "Can't score $ARGV with envelope: " . CMAE_StrError($err) . "\n"
      if $err;

    warn "FYI: dummy envelope changed the score from $score to $e_score\n"
      if $score != $e_score;

    # Map category numbers to names
    my ($cat_desc, $sub_cat_desc);

    $cli->describe_category(category => $category, sub_category => $sub_category,
                            out_category_desc => \$cat_desc, out_sub_category_desc => \$sub_cat_desc);

    # Note that we don't bother printing the analysis header.
    print "$ARGV: score=$score category=$category ($cat_desc) sub_category=$sub_category ($sub_cat_desc) rescan=$rescan\n";
}

undef $cli;  # force close

