diff --git a/app.cfg b/app.cfg index 133e814..f4a3166 100644 --- a/app.cfg +++ b/app.cfg @@ -1,6 +1,7 @@ [features] memcached=true evalserver=true +blogspam=false [memcached] namespace="pastebin" @@ -10,17 +11,20 @@ hash_namespace=1 io_timeout=0.5 max_failures=3 failure_timeout=2 - max_size=524288 -[[memcached.servers]] -address="localhost:11211" -weight="2.5" +[blogspam] +site="https://perlbot.pl/" [evalserver] server="localhost:14400" languages=[ "perl" ] + +[[memcached.servers]] +address="localhost:11211" +weight="2.5" + # Config for a non-local server. Has a lower weight so that the local one gets checked first # [[memcached.servers]] # address="remotehost:11211" diff --git a/app.pl b/app.pl index 9989131..4e2aaba 100755 --- a/app.pl +++ b/app.pl @@ -15,12 +15,12 @@ use Encode qw/decode/; use Mojolicious::Lite; use Mojolicious::Plugin::TtRenderer; use POE::Filter::Reference; - use App::Config; use App::Memcached; use Eval::Perlbot; use IRC::Perlbot; use DateTime; +use App::Spamfilter; plugin 'tt_renderer' => { template_options => { @@ -32,6 +32,10 @@ plugin 'tt_renderer' => { app->renderer->default_handler( 'tt' ); +if ($cfg->{features}{blogspam}) { + plugin 'BlogSpam' => ($cfg->{blogspam}->%*); +} + my $dbh = DBI->connect("dbi:SQLite:dbname=pastes.db", "", "", {RaiseError => 1}); $dbh->{sqlite_unicode} = 1; # hardcode some channels first @@ -60,11 +64,16 @@ post '/paste' => sub { my @args = map {($c->param($_))} qw/paste name desc chan/; my $id = insert_pastebin(@args); + my ($code, $who, $desc, $channel) = @args; # TODO select which one based on config # TODO make this use the config, or http params for the url - my ($channel, $who, $what, $link) = @_; - IRC::Perlbot::announce($c->param('chan'), $c->param('name'), $c->param('desc'), "https://perlbot.pl/pastebin/$id"); + + if (my $type = App::Spamfilter::is_spam($c, $who, $desc, $code)) { + warn "I thought this was spam! $type"; + } else { + IRC::Perlbot::announce($c->param('chan'), $c->param('name'), $c->param('desc'), "https://perlbot.pl/pastebin/$id"); + } $c->redirect_to('/pastebin/'.$id); #$c->render(text => "post accepted! $id"); @@ -124,7 +133,6 @@ post '/eval' => sub { get '/robots.txt' => sub { my ($c) = @_; - $c->render(text => qq{User-agent: * Disallow: /}); }; diff --git a/cpanfile b/cpanfile index 7f7d63b..48802f6 100644 --- a/cpanfile +++ b/cpanfile @@ -1,11 +1,12 @@ requires 'Mojolicious'; requires 'JSON::MaybeXS'; -requires 'TOML' +requires 'TOML'; requires 'Cache::Memcached::Fast'; requires 'DateTime'; requires 'DBI'; requires 'DBD::SQLite'; requires 'Mojolicious::Lite'; requires 'Mojolicious::Plugin::TtRenderer'; +requires 'Mojolicious::Plugin::BlogSpam'; requires 'POE::Filter::Reference'; diff --git a/lib/App/Spamfilter.pm b/lib/App/Spamfilter.pm new file mode 100644 index 0000000..b21994e --- /dev/null +++ b/lib/App/Spamfilter.pm @@ -0,0 +1,25 @@ +package App::Spamfilter; + +use strict; +use warnings; +use App::Config; + +sub is_spam { + my ($c, $who, $what, $code) = @_; + + if ($cfg->{features}{blogspam}) { + my $blogspam = $c->blogspam( + comment => $code, + subject => $what, + name => $who + ); + + return 1 unless ($blogspam->test_comment()); + } + + return 2 if ($who =~ /^[A-Z]\w+\s+[A-Z]\w+$/); # block proper names, probably spam + return 3 if ($what =~ m|https?://|); # no links in the desc, maybe relax later + return 0; # we thought it wasn't spam +} + +1; diff --git a/pastes.db b/pastes.db index 2c3ae0d..7aade41 100644 Binary files a/pastes.db and b/pastes.db differ