Mojolicious-Plugin-BlogSpam

 view release on metacpan or  search on metacpan

lib/Mojolicious/Plugin/BlogSpam.pm  view on Meta::CPAN

use Mojo::Base 'Mojolicious::Plugin';
use Mojo::URL;
use Mojo::JSON;
use Mojo::Log;
use Mojo::UserAgent;
use Mojo::IOLoop;
use Scalar::Util 'weaken';

our $VERSION = '0.12';

# TODO: X-Forwarded-For in Config per index steuern
# TODO: - Check for blacklist/whitelist/max words etc. yourself.
#       - Create a route condition for posts.
#         -> $r->post('/comment')->over('blogspam')->to('#');

our @OPTION_ARRAY =
  qw/blacklist exclude whitelist mandatory
     max-links max-size min-size min-words/;
     # 'fail' is special, as it is boolean


lib/Mojolicious/Plugin/BlogSpam.pm  view on Meta::CPAN

      # Get request headers
      my $headers = $c->req->headers;

      # Set user-agent if not given
      $obj->agent($headers->user_agent) unless $obj->agent;

      # No ip manually given
      unless ($obj->ip) {

        # Get forwarded ip
        if (my $ip = $headers->to_hash->{'X-Forwarded-For'}) {
          $obj->ip( split(/\s*,\s*/, $ip) );
        };

        # Get host ip, because X-Forwarded-For wasn't set
        unless ($obj->ip) {
          $obj->ip( split(/\s*:\s*/, ($headers->host || '')) );
        };
      };

      # Return blogspam object
      return $obj;
    }
  );
};

lib/Mojolicious/Plugin/BlogSpam.pm  view on Meta::CPAN

Returns a hash representation of the comment.


=head2 ip

  $bs->ip('192.168.0.1');
  my $ip = $bs->ip;

The ip address of the commenter.
Defaults to the ip address of the request.
Supports C<X-Forwarded-For> proxy information.


=head2 link

  $bs->link('http://grimms-abenteuer.de/');
  my $link = $bs->link;

Homepage link given by the commenter.


t/blogspam.t  view on Meta::CPAN

$header->host('192.168.0.1:1234');
$header->user_agent('New client');

ok($bs = $c->blogspam(
  comment => 'This is a test post'
), 'Blogspam');

is($bs->ip, '192.168.0.1', 'IP');
is($bs->agent, 'New client', 'Agent');

$header->add('X-Forwarded-For' => '192.168.0.2, 192.168.0.3');

ok($bs = $c->blogspam(
  comment => 'This is a test post'
), 'Blogspam');

is($bs->ip, '192.168.0.2', 'X-Forwarded-For');

my $opt = $bs->_options(mandatory => 'email');
like($opt, qr/exclude=badip/,     'Option String 1');
like($opt, qr/mandatory=subject/, 'Option String 2');
like($opt, qr/mandatory=name/,    'Option String 3');
like($opt, qr/mandatory=email/,   'Option String 4');

memory_cycle_ok($app);

done_testing;



( run in 0.264 second using v1.01-cache-2.11-cpan-26ccb49234f )