Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

[HELP] Spam checking [PERL]

Name: Anonymous 2009-03-08 5:38

I'M NOT GOOD WITH COMPUTERS

sub spamcheck() {
open(SPAM, "<spam.txt") || die 'MISSING';
@raw_data=<SPAM>;
close(SPAM);
for(@raw_data) {
  /^(.*)$/;
  my $blocked = $1;
  die 'SPAM' if $comment =~ $blocked;
 }
}


I'm trying to make some sort of spamchecker so when a person submits a comment, it is checked against spam.txt and if it doesn't match any of the lines, then it is allowed to continute.

However this bit of code isn't working for me. For example, I have spam.txt with several lines of test words, the first line is derp and when I submit a comment with the word derp it doesn't stop the comment from being submitted. However, when I submit a comment with derp and then a newline, it errors as it's supposed to. But it only does with with the first line. The other lines in spam.txt don't trigger it at all.

Please help me.

Name: Anonymous 2009-03-08 5:43

HURR DURR I DON'T KNOW HOW TO REMOVE A NEWLINE CHARACTER HURRRRR

Name: Anonymous 2009-03-08 5:46

IHBT

Name: Anonymous 2009-03-08 5:50

you only took the first line.
try while(<SPAM>)
or alternatively insert local $/ before @raw_data=<SPAM>;

Name: Anonymous 2009-03-08 5:52

>>4
actually, that second one probably won't work with your code; don't bother

Name: Anonymous 2009-03-08 6:41

download/buy & read a copy of learning Perl

Name: Anonymous 2009-03-08 8:55

Use chomp to remove newlines.

Also $comment =~ $blocked; should be $comment =~ /$blocked/;

Name: Anonymous 2009-03-08 9:01

this is retarded

Name: Anonymous 2009-03-08 10:42

OP here, I changed my mind:

Don't help me.

Name: Anonymous 2009-03-08 10:58

>>9
Not me.

OP here, thanks for your suggestions, I'm going to try them out.

I'll post later, I'm going to go hike some hills first.

Name: Anonymous 2009-03-08 11:25

sub spamcheck() {
  while (<SPAM>) {
    chomp;
    die 'SPAM' if($comment =~ /$_/)
  } or die 'MISSING';
}

Name: Anonymous 2009-03-08 12:23

As in >>11.


That reminds me, I had a weird problem with the Data::Dumper output of some hash references if I chomped the elements. Maybe I'll post it later.

Name: Anonymous 2009-03-08 21:41

None of these suggestions seem to work. In fact, I've come to realize that the code I originally posted actually works, with the exception of having to have a newline after the blacklisted item in order to trigger the spam filter hours after you guys had already told me. I just really needed time to let it sink in after experimenting with it. Turns out I was doing it wrong in spam.txt by not putting &lt; instead of < and [ instead of [ so the spam filter would not trigger, obviously.

Also, this is weird, I took my script to another server to test it and it works perfectly, no newline issues. I was not expecting that.

Name: Anonymous 2009-03-08 21:42

[ instead of [

Oh right, \ doesn't show up in italics, silly BBCode.

Name: Anonymous 2009-03-09 2:34

This blows, chomp doesn't seem to be doing its thing.

sub spamcheck() {
open(SPAM, "<spam.txt") || die 'spam.txt is missing';
while (<SPAM>) {
  /^(.*?)$/;
  my ($blocked) = ($1);
  chomp($blocked);
  die 'Your post was detected as spam' if $comment =~ /$blocked/i;
 }
}


What am I doing wrong? Spamcheck still isn't triggered unless the blacklisted item is followed by a new line. CHOOOOOOOOMMMMMMP!!!

Name: Anonymous 2009-03-09 3:07

>>15
maybe your spam.txt is a dos text file and perl is reading it as a unix text file?
"spam\r\n" -> /^(.*?)$/ -> "spam\r"

Name: Anonymous 2009-03-09 3:32

Build a suffix tree.

Name: Anonymous 2009-03-09 4:56

wtf is wrong with you, anyone on here should be able to write something this simple in perl without even thinking

i'm at work, on windows, with restricted network access and no perl interprter but i'm going to write it for you

#!/usr/bin/perl

use warnings;
use strict;

sub spamcheck($) {
  my $string = shift;
  open(SPAM, "spam.txt") or die($!);
  while(<SPAM>) {
    return(-1) if($string =~ m/$_/);
  }
  close(SPAM);
  return(1);
}

while(<>) {
  die("SPAM WARNING") unless(spamcheck($_) == 1);
}

print "no spam found...\n";

exit(0);

Name: Anonymous 2009-03-09 4:59

>>18
perl
without even thinking
( ≖‿≖)

Name: Anonymous 2009-03-09 5:18

>>18
Perl can run just fine on Windows.

Name: Anonymous 2009-03-09 5:35

>>20
Yeah, only if everything is installed. But it still runs better on *nix.

Name: Anonymous 2009-03-09 7:43

>>18
The real question is: Why would anyone want to write in outdated languages like Perl when they could write it much faster and less error-prone in a more modern language?

Next you'll suggest actually doing non-toy projects in LISP. Hahaha.

Name: Anonymous 2009-03-09 8:27

>>22
1/10
Good look with coding that in bbcode.

Name: Anonymous 2009-03-09 10:48

>>18
Well, sir, it seems you could not. That bit did not work at all. I don't hold it against you, given the situation you were in.

>>16
You, on the other hand, are a genius.

I amended the code as such:
sub spamcheck() {
open(SPAM, "<spam.txt") || die 'spam.txt is missing';
while (<SPAM>) {
  /^(.*?)(\r*\n*)$/;
  die 'Your post was detected as spam' if $comment =~ /$1/i;
 }
}


It works perfectly now! Thank you

Name: Anonymous 2009-03-09 10:55

STOP HELPING PEOPLE!!!

Name: Anonymous 2009-03-09 18:54

>>25
GO BACK TO /b/!!!

Name: Anonymous 2010-12-26 0:00

Name: Anonymous 2011-02-03 4:01

Don't change these.
Name: Email:
Entire Thread Thread List