Skip to content

Commit

Permalink
Merge branch 'master' of github.com:courtenay/splam
Browse files Browse the repository at this point in the history
Conflicts:
	lib/splam/rule.rb
	lib/splam/rules/bad_words.rb
	lib/splam/rules/good_words.rb
	lib/splam/rules/line_length.rb
  • Loading branch information
courtenay committed Dec 12, 2012
2 parents a6e4faa + 1d1ba1a commit 27c2536
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/splam/rule.rb
Expand Up @@ -32,6 +32,7 @@ def run(*args)


def initialize(suite, record, weight = 1.0, request = nil) def initialize(suite, record, weight = 1.0, request = nil)
@suite, @weight, @score, @reasons, @body, @request = suite, weight, 0, [], record.send(suite.body), request @suite, @weight, @score, @reasons, @body, @request = suite, weight, 0, [], record.send(suite.body), request
@user = record.user # todo: customize user field
end end


def name def name
Expand Down
1 change: 1 addition & 0 deletions lib/splam/rules/bad_words.rb
Expand Up @@ -66,6 +66,7 @@ def run
@body.scan(/<a(.*?)>/).each do |match| @body.scan(/<a(.*?)>/).each do |match|
add_score self.class.bad_word_score * 10 * match[0].scan(word).size, "nasty word inside a URL: #{word}" add_score self.class.bad_word_score * 10 * match[0].scan(word).size, "nasty word inside a URL: #{word}"
end end

end end
if counter > (wordlist.size / 2) if counter > (wordlist.size / 2)
add_score 1000, "Lots of bad words from one genre (#{key}): #{counter}" add_score 1000, "Lots of bad words from one genre (#{key}): #{counter}"
Expand Down
2 changes: 1 addition & 1 deletion lib/splam/rules/chinese.rb
Expand Up @@ -2,7 +2,7 @@ class Splam::Rules::Chinese < Splam::Rule
class << self class << self
attr_accessor :base_score attr_accessor :base_score
end end
self.base_score = 3 self.base_score = 5


def run def run
banned_words =[ # various chinese characters banned_words =[ # various chinese characters
Expand Down
2 changes: 1 addition & 1 deletion lib/splam/rules/good_words.rb
Expand Up @@ -7,8 +7,8 @@ def run
good_words |= %w( redirect login diff dreamhost setup subversion git wildcard domain subdomain ssh database ) good_words |= %w( redirect login diff dreamhost setup subversion git wildcard domain subdomain ssh database )
good_words |= %w( project billing tags description comment milestone saving happening feature mac implement report) good_words |= %w( project billing tags description comment milestone saving happening feature mac implement report)
good_words |= %w( rss notification subscribe calendar chart note task gantt search service ownership application communicate ) good_words |= %w( rss notification subscribe calendar chart note task gantt search service ownership application communicate )
good_words |= %w( pattern template web integer status xml activereload html state page)
good_words |= %w( interaction API tickets hosted domain skitch ) good_words |= %w( interaction API tickets hosted domain skitch )
good_words |= %w( pattern template web integer status xml activereload html state page rack diff )
good_words << "project management" good_words << "project management"
good_words << "/usr/local/lib" << "gems" good_words << "/usr/local/lib" << "gems"


Expand Down
26 changes: 26 additions & 0 deletions lib/splam/rules/line_length.rb
@@ -0,0 +1,26 @@
class Splam::Rules::LineLength < Splam::Rule

def name
"Line length"
end

# Penalize long line lengths.
def run
lines = @body.split("\n")
lines.each do |line|
next if line =~ /\A\s{4,}/ # ignore code blocks

# multiplier = (lines.size == 1) ? 10 : 1 # one line? fail.
multiplier = 1

# 1 point for each 40 chars in a line.
hits = (line.size / 40) * multiplier
add_score hits, "lines over 40 chars"

# 2 more points if line is longer than 80
hits = (line.size / 80) * 2 * multiplier
add_score hits, "lines over 80 chars"

end
end
end
15 changes: 15 additions & 0 deletions lib/splam/rules/user.rb
@@ -0,0 +1,15 @@
class Splam::Rules::User < Splam::Rule

def run
bad_words = ["qq.com", "yahoo.cn", "126.com"]
bad_words |= %w( mortgage )

bad_words.each do |word|
add_score 50, "User's email address has suspicious parts: #{word}" if @user.email.include?(word)
end

add_score "20", "User has lots and lots of dots" if @user.email.split("@")[0].scan(/\./).size > 5

add_score 5, "User is untrusted" if !@user.trusted?
end
end
15 changes: 7 additions & 8 deletions lib/splam/rules/word_length.rb
Expand Up @@ -16,17 +16,16 @@ def median(arr)


def run def run
words = [] words = []
words = @body.split(/\s/).map do |word| words = @body.split(/\s/)
word.size words.delete_if { |w| w =~ /^https?\:\/\// }
end words.collect! { |word| word.size }
words.delete_if { |w| w =~ /^http\:\/\//}


# Only count word lengths over 10 # Only count word lengths over 10
if words.size > 5 if words.size > 5
add_score 20, "Average word length over 5" if average(words) > 5 add_score 5, "Average word length over 5" if average(words) > 5
add_score 50, "Average word length over 10" if average(words) > 10 add_score 10, "Average word length over 10" if average(words) > 10
add_score 10, "Median word length over 5" if median(words) > 5 add_score 5, "Median word length over 5" if median(words) > 5
add_score 50, "Median word length over 10" if median(words) > 10 add_score 10, "Median word length over 10" if median(words) > 10
end end
end end
end end
22 changes: 20 additions & 2 deletions test/splam_test.rb
Expand Up @@ -7,13 +7,26 @@ def run
end end
end end


class User
attr_accessor :trusted
def trusted?
trusted
end
def email
"test@test.com"
end
end

class Foo class Foo
include ::Splam include ::Splam
splammable :body splammable :body
attr_accessor :body attr_accessor :body
def body def body
@body || "This is body\320\224 \320\199" @body || "This is body\320\224 \320\199"
end end
def user
User.new
end
end end


class FooCond class FooCond
Expand All @@ -27,7 +40,9 @@ class PickyFoo
splammable :body do |s| splammable :body do |s|
s.rules = [:fixed_rule, FixedRule] s.rules = [:fixed_rule, FixedRule]
end end

def user
User.new
end
def body def body
'lol wut' 'lol wut'
end end
Expand All @@ -38,6 +53,9 @@ class HeavyFoo
splammable :body do |s| splammable :body do |s|
s.rules = {:fixed_rule => 3} s.rules = {:fixed_rule => 3}
end end
def user
User.new
end


def body def body
'lol wut' 'lol wut'
Expand All @@ -47,7 +65,7 @@ def body
def test_runs_plugins def test_runs_plugins
f = Foo.new f = Foo.new
assert ! f.splam? assert ! f.splam?
assert_equal 35, f.splam_score assert_equal 40, f.splam_score
end end


def test_runs_plugins_with_specified_rules def test_runs_plugins_with_specified_rules
Expand Down

0 comments on commit 27c2536

Please sign in to comment.