package Eval1;

use strict;
use warnings;

use base 'Mail::SpamAssassin::Plugin';

sub new {
    my ( $class, $mailsa ) = @_;

    $class = ref($class) || $class;
    my $self = $class->SUPER::new($mailsa);
    bless $self, $class;

    ### register the eval rule
    $self->register_eval_rule("check_stockspam");

    return $self;
}

my $stock_token_regex = qr{
    (?:\b|_)                            # word boundary, or _
    (?i:Inc|Corp(?:oration)?|Gr[0o]up)  # "inc" or something
    [.,]?                               # optional dot or comma
    \s*\n                               # match to end of line
    ^\s*(?i:Sym\w*\s*:\s*)?             # next line optional sym(bol):
    (                                   # start capture
        (?:[A-Z]\s{0,3}){3}             # match three letters, optional spaces
        [A-Z]                           # match last letter
    )                                   # end capture
    \s*$                                # match until end of line
}xsm;

my %ticker_symbol = map { $_ => 1 } qw(
    MRDY LVCC NNCP CTCX
);

sub check_stockspam {
    my ( $self, $per_msg_status, $body_ref ) = @_;

    my $msg_text = join( '', @$body_ref );
    while ( my ($possible_symbol) = $msg_text =~ /$stock_token_regex/ ) {
        ### trim all non-letters
        $possible_symbol =~ tr/A-Za-z//cd;
        return 1 if exists $ticker_symbol{$possible_symbol};
    }
    return 0;
}

1;
