Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

filter spams - mailinglist, registrations

  • Loading branch information...
commit 906ea3802cd7037d2673efbcf9ba5dd041e95135 1 parent 6238cd3
progressbarsk authored
View
5 db/migrate/20120930065241_add_spam_to_mailinglists.rb
@@ -0,0 +1,5 @@
+class AddSpamToMailinglists < ActiveRecord::Migration
+ def change
+ add_column ::Refinery::Mailinglists::Subscriber.table_name, :spam, :boolean
+ end
+end
View
5 db/migrate/20120930084257_add_spam_to_registrations.rb
@@ -0,0 +1,5 @@
+class AddSpamToRegistrations < ActiveRecord::Migration
+ def change
+ add_column :refinery_registrations, :spam, :boolean
+ end
+end
View
4 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120910012516) do
+ActiveRecord::Schema.define(:version => 20120930084257) do
create_table "refinery_blog_categories", :force => true do |t|
t.string "title"
@@ -207,6 +207,7 @@
t.boolean "events", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.boolean "spam"
end
add_index "refinery_mailinglists_subscribers", ["email"], :name => "index_refinery_mailinglists_subscribers_on_email", :unique => true
@@ -281,6 +282,7 @@
t.string "state", :default => "unmoderated", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.boolean "spam"
end
add_index "refinery_registrations", ["id"], :name => "index_refinery_registrations_on_id"
View
38 ...r/extensions/mailinglists/app/controllers/refinery/mailinglists/subscribers_controller.rb
@@ -24,23 +24,31 @@ def create
subscribed_general = false
subscribed_events = false
if subscriber.nil? and @subscriber.valid?
- subscribed_general = subscribe(@subscriber, 'general') if @subscriber[:general]
- subscribed_events = subscribe(@subscriber, 'events') if @subscriber[:events]
+ if @subscriber.spam?
+ subscribed_events = true # ignore spam
+ else
+ subscribed_general = subscribe(@subscriber, 'general') if @subscriber[:general]
+ subscribed_events = subscribe(@subscriber, 'events') if @subscriber[:events]
- Mailer.notification(@subscriber, request).deliver if (subscribed_general or subscribed_events)
+ Mailer.notification(@subscriber, request).deliver if (subscribed_general or subscribed_events)
+ end
else
- # otherwise we try subscribe or unsubscribe him from what he want
- if @subscriber[:general]
- if subscriber[:general]
- unsubscribe(subscriber, 'general') and return
- else
- subscribed_general = subscribe(subscriber, 'general')
- end
- elsif @subscriber[:events]
- if subscriber[:events]
- unsubscribe(subscriber, 'events') and return
- else
- subscribed_events = subscribe(subscriber, 'events')
+ if @subscriber.spam?
+ subscribed_events = true # ignore spam
+ else
+ # otherwise we try subscribe or unsubscribe him from what he want
+ if @subscriber[:general]
+ if subscriber[:general]
+ unsubscribe(subscriber, 'general') and return
+ else
+ subscribed_general = subscribe(subscriber, 'general')
+ end
+ elsif @subscriber[:events]
+ if subscriber[:events]
+ unsubscribe(subscriber, 'events') and return
+ else
+ subscribed_events = subscribe(subscriber, 'events')
+ end
end
end
end
View
71 vendor/extensions/mailinglists/app/models/refinery/mailinglists/subscriber.rb
@@ -14,6 +14,75 @@ class Subscriber < Refinery::Core::BaseModel
:uniqueness => true,
:length => { :in => 6..64 },
:format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
+
+ scope :ham, :conditions => {:spam => false}
+ scope :spam, :conditions => {:spam => true}
+
+ before_validation(:on => :create) { |spammable| spammable.send(:calculate_spam_score) }
+
+ cattr_accessor :spam_words
+ self.spam_words = %w{
+ -online 4u 4-u acne adipex advicer baccarrat blackjack bllogspot booker buy byob carisoprodol
+ casino chatroom cialis coolhu credit-card-debt cwas cyclen cyclobenzaprine orgy
+ day-trading debt-consolidation discreetordering duty-free dutyfree equityloans fioricet
+ freenet free\s*shipping gambling- hair-loss homefinance holdem incest jrcreations leethal levitra macinstruct
+ mortgagequotes nemogs online-gambling ottawavalleyag ownsthis paxil penis pharmacy phentermine
+ poker poze pussy ringtones roulette shemale shoes -site slot-machine thorcarlson
+ tramadol trim-spa ultram valeofglamorganconservatives viagra vioxx xanax zolus adult
+ }
+
+ def ham?
+ not spam?
+ end
+
+ def ham!
+ self.update_attribute(:spam, false)
+ end
+
+ def spam!
+ self.update_attribute(:spam, true)
+ end
+
+ protected
+
+ def score_for_previous_submissions
+ current_score = 0
+
+ self.class.find(:all, :conditions => {:email => self[:email]}).each do |i|
+ if i.spam?
+ current_score -= 1
+ else
+ current_score += 1
+ end
+ end
+
+ current_score
+ end
+
+ def score_for_spam_words
+ current_score = 0
+
+ spam_words.each do |word|
+ regex = /#{word}/i
+ if self[:email] =~ regex
+ current_score -= 1
+ end
+ end
+
+ current_score
+ end
+
+ def calculate_spam_score
+ score = 0
+ score += score_for_previous_submissions
+ score += score_for_spam_words
+ self.spam = (score < 0)
+
+ logger.info("spam score was #{score}")
+
+ true
+ end
end
+
end
-end
+end
View
4 ...tensions/registrations/app/controllers/refinery/registrations/registrations_controller.rb
@@ -17,12 +17,12 @@ def create
if @registration.save
begin
- Mailer.notification(@registration, request).deliver
+ Mailer.notification(@registration, request).deliver unless @registration.spam? # ignore spams
Mailer.confirmation(@registration, request).deliver
rescue => e
logger.warn "There was an error delivering the registration notification/confirmation:\n#{e.message}\n"
end
-
+
redirect_to refinery.thank_you_registrations_registrations_path
else
flash[:error] = t('some_error_happen_on_registration_html',
View
8 vendor/extensions/registrations/app/models/refinery/registrations/registration.rb
@@ -23,6 +23,12 @@ class Registration < Refinery::Core::BaseModel
scope :approved, :conditions => {:state => 'approved'}
scope :rejected, :conditions => {:state => 'rejected'}
+ filters_spam({
+ :message_field => :message,
+ :email_field => :email,
+ :author_field => :username
+ })
+
def approve!
self.update_attribute(:state, 'approved')
end
@@ -43,7 +49,7 @@ def unmoderated?
self.state == 'unmoderated'
end
- private
+ private
def member_with_email_exists?
errors.add(:base, ::I18n.t(:member_exists_html,
Please sign in to comment.
Something went wrong with that request. Please try again.