# $HeadURL$
# $LastChangedRevision$
# $LastChangedDate$
# $LastChangedBy$
#
package OpenGuides::Local::CamGuideSpam;

use strict;

use Data::Dumper;
use MIME::Lite;

sub looks_like_spam {
    my ( $class, %args ) = @_;

    my $name = $args{node};
    my $content = $args{content};
    my $host = $args{host};
    my @cats = @{ $args{metadata}{category} || [] };
    my @locs = @{ $args{metadata}{locale} || [] };

    if ( $name eq "Category Board Games"
         && $content =~ m'http://.*http://.*http://'is ) {
#        $class->notify_admins( %args,
#            id => "00001",
#            reason => "Edit to board games page with more than two URLs in." );
        return 1;
    }

    if ( $name eq "Station Tandoori" && $host eq "82.6.101.64" ) {
#        $class->notify_admins( %args,
#            id => "00002",
#            reason => "82.6.101.64 editing Station Tandoori." );
        return 1;
    }

    if ( $content =~ /\b(viagra|cialis|supermeganah|tramadol|vicodin|phentermine|zoloft|lolita)\b/is ) {
#        $class->notify_admins( %args,
#            id => "00003",
#            reason => "Matches $1" );
        return 1;
    }

    foreach my $cat ( @cats ) {
        if ( $cat =~ m'http://'i ) {
#            $class->notify_admins( %args,
#                id => "00004",
#                reason => "URL in category field" );
            return 1;
        }
    }

    foreach my $loc ( @locs ) {
        if ( $loc =~ m'http://'i ) {
#            $class->notify_admins( %args,
#                id => "00005",
#                reason => "URL in locales field" );
            return 1;
        }
    }

    if ( ( scalar @cats == 1 ) && ( scalar @locs == 1 )
         && $cats[0] eq $locs[0] ) {
#        $class->notify_admins( %args,
#            id => "00006",
#            reason => "category and locale identical" );
        return 1;
     }
}

sub notify_admins {
    my ( $class, %args ) = @_;
    my $reason = delete $args{reason};
    my $id = delete $args{id};
    my $message = MIME::Lite->new(
        From     => 'kake@earth.li',
        To       => 'kake@earth.li',
        Subject  => "Attempted spam edit on OGCam",
        Data     => "Someone just tried to edit OGCam, and I said no because "
                    . "it looked like spam.\n\nReason: $reason\n\nID: $id\n\n"
                    . "Here's a dump of the details:\n"
                    . Dumper( \%args ),
    );
    $message->send;
}

1;
