Permalink
Browse files

Merge branch 'master' of github.com:courtenay/splam

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...
2 parents a6e4faa + 1d1ba1a commit 27c25365a72d603d0cc3e112dc4b91a020cdec49 @courtenay committed Dec 12, 2012
View
@@ -32,6 +32,7 @@ def run(*args)
def initialize(suite, record, weight = 1.0, request = nil)
@suite, @weight, @score, @reasons, @body, @request = suite, weight, 0, [], record.send(suite.body), request
+ @user = record.user # todo: customize user field
end
def name
@@ -66,6 +66,7 @@ def run
@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}"
end
+
end
if counter > (wordlist.size / 2)
add_score 1000, "Lots of bad words from one genre (#{key}): #{counter}"
@@ -2,7 +2,7 @@ class Splam::Rules::Chinese < Splam::Rule
class << self
attr_accessor :base_score
end
- self.base_score = 3
+ self.base_score = 5
def run
banned_words =[ # various chinese characters
@@ -7,8 +7,8 @@ def run
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( 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( pattern template web integer status xml activereload html state page rack diff )
good_words << "project management"
good_words << "/usr/local/lib" << "gems"
@@ -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
View
@@ -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
@@ -16,17 +16,16 @@ def median(arr)
def run
words = []
- words = @body.split(/\s/).map do |word|
- word.size
- end
- words.delete_if { |w| w =~ /^http\:\/\//}
+ words = @body.split(/\s/)
+ words.delete_if { |w| w =~ /^https?\:\/\// }
+ words.collect! { |word| word.size }
# Only count word lengths over 10
if words.size > 5
- add_score 20, "Average word length over 5" if average(words) > 5
- add_score 50, "Average word length over 10" if average(words) > 10
- add_score 10, "Median word length over 5" if median(words) > 5
- add_score 50, "Median word length over 10" if median(words) > 10
+ add_score 5, "Average word length over 5" if average(words) > 5
+ add_score 10, "Average word length over 10" if average(words) > 10
+ add_score 5, "Median word length over 5" if median(words) > 5
+ add_score 10, "Median word length over 10" if median(words) > 10
end
end
end
View
@@ -7,13 +7,26 @@ def run
end
end
+ class User
+ attr_accessor :trusted
+ def trusted?
+ trusted
+ end
+ def email
+ "test@test.com"
+ end
+ end
+
class Foo
include ::Splam
splammable :body
attr_accessor :body
def body
@body || "This is body\320\224 \320\199"
end
+ def user
+ User.new
+ end
end
class FooCond
@@ -27,7 +40,9 @@ class PickyFoo
splammable :body do |s|
s.rules = [:fixed_rule, FixedRule]
end
-
+ def user
+ User.new
+ end
def body
'lol wut'
end
@@ -38,6 +53,9 @@ class HeavyFoo
splammable :body do |s|
s.rules = {:fixed_rule => 3}
end
+ def user
+ User.new
+ end
def body
'lol wut'
@@ -47,7 +65,7 @@ def body
def test_runs_plugins
f = Foo.new
assert ! f.splam?
- assert_equal 35, f.splam_score
+ assert_equal 40, f.splam_score
end
def test_runs_plugins_with_specified_rules

0 comments on commit 27c2536

Please sign in to comment.