A platform for community discussion. Free, open, simple.
FEATURE: Create hidden posts for received spam emails (#6010)
Patrick Gansterer28dd7fb56231
diff --git a/app/models/post.rb b/app/models/post.rbindex 14d57b824ef..99cc14b7dc7 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -113,7 +113,8 @@ class Post < ActiveRecord::Base@hidden_reasons ||= Enum.new(flag_threshold_reached: 1,flag_threshold_reached_again: 2,new_user_spam_threshold_reached: 3,- flagged_by_tl3_user: 4)+ flagged_by_tl3_user: 4,+ email_spam_header_found: 5)enddef self.typesdiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 3b6a93ea90f..9b5f0e82074 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1541,6 +1541,7 @@ en:log_mail_processing_failures: "Log all email processing failures to http://yoursitename.com/logs"email_in: "Allow users to post new topics via email (requires manual or pop3 polling). Configure the addresses in the \"Settings\" tab of each category."email_in_min_trust: "The minimum trust level a user needs to have to be allowed to post new topics via email."+ email_in_spam_header: "The email header to detect spam."email_prefix: "The [label] used in the subject of emails. It will default to 'title' if not set."email_site_title: "The title of the site used as the sender of emails from the site. Default to 'title' if not set. If your 'title' contains characters that are not allowed in email sender strings, use this setting."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 8b2b3c8648d..d04516e9743 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -790,6 +790,13 @@ email:email_in_min_trust:default: 2enum: 'TrustLevelSetting'+ email_in_spam_header:+ type: enum+ default: 'none'+ choices:+ - none+ - X-Spam-Flag+ - X-Spam-Statusemail_prefix: ''email_site_title: ''disable_emails:diff --git a/lib/email/receiver.rb b/lib/email/receiver.rbindex 260918bfe71..50735806d2d 100644--- a/lib/email/receiver.rb+++ b/lib/email/receiver.rb@@ -111,6 +111,8 @@ module Emailraise FromReplyByAddressError if is_from_reply_by_email_address?raise ScreenedEmailError if ScreenedEmail.should_block?(@from_email)+ hidden_reason_id = is_spam? ? Post.hidden_reasons[:email_spam_header_found] : nil+user = find_user(@from_email)if user.present?@@ -149,6 +151,7 @@ module Emailcreate_reply(user: user,raw: body,elided: elided,+ hidden_reason_id: hidden_reason_id,post: post,topic: post.topic,skip_validations: user.staged?)@@ -157,7 +160,7 @@ module Emaildestinations.each do |destination|begin- process_destination(destination, user, body, elided)+ process_destination(destination, user, body, elided, hidden_reason_id)rescue => efirst_exception ||= eelse@@ -243,6 +246,17 @@ module Email@mail.header.to_s[/auto[\-_]?(response|submitted|replied|reply|generated|respond)|holidayreply|machinegenerated/i]end+ def is_spam?+ case SiteSetting.email_in_spam_header+ when 'X-Spam-Flag'+ @mail[:x_spam_flag].to_s[/YES/i]+ when 'X-Spam-Status'+ @mail[:x_spam_status].to_s[/^Yes, /i]+ else+ false+ end+ end+def select_bodytext = nilhtml = nil@@ -541,7 +555,7 @@ module Emailnilend- def process_destination(destination, user, body, elided)+ def process_destination(destination, user, body, elided, hidden_reason_id)return if SiteSetting.enable_forwarded_emails &&has_been_forwarded? &&process_forwarded_email(destination, user)@@ -549,7 +563,7 @@ module Emailcase destination[:type]when :groupgroup = destination[:obj]- create_group_post(group, user, body, elided)+ create_group_post(group, user, body, elided, hidden_reason_id)when :categorycategory = destination[:obj]@@ -560,6 +574,7 @@ module Emailcreate_topic(user: user,raw: body,elided: elided,+ hidden_reason_id: hidden_reason_id,title: subject,category: category.id,skip_validations: user.staged?)@@ -574,13 +589,14 @@ module Emailcreate_reply(user: user,raw: body,elided: elided,+ hidden_reason_id: hidden_reason_id,post: email_log.post,topic: email_log.post.topic,skip_validations: user.staged?)endend- def create_group_post(group, user, body, elided)+ def create_group_post(group, user, body, elided, hidden_reason_id)message_ids = Email::Receiver.extract_reply_message_ids(@mail, max_message_id_count: 5)post_ids = []@@ -598,6 +614,7 @@ module Emailcreate_reply(user: user,raw: body,elided: elided,+ hidden_reason_id: hidden_reason_id,post: post,topic: post.topic,skip_validations: true)@@ -605,6 +622,7 @@ module Emailcreate_topic(user: user,raw: body,elided: elided,+ hidden_reason_id: hidden_reason_id,title: subject,archetype: Archetype.private_message,target_group_names: [group.name],diff --git a/lib/post_creator.rb b/lib/post_creator.rbindex 8999d58eb06..ddeda46d55a 100644--- a/lib/post_creator.rb+++ b/lib/post_creator.rb@@ -36,6 +36,7 @@ class PostCreator# wrap `PostCreator` in a transaction, as the sidekiq jobs could# dequeue before the commit finishes. If you do this, be sure to# call `enqueue_jobs` after the transaction is comitted.+ # hidden_reason_id - Reason for hiding the post (optional)## When replying to a topic:# topic_id - topic we're replying to@@ -62,6 +63,7 @@ class PostCreatoropts[:title] = pg_clean_up(opts[:title]) if opts[:title] && opts[:title].include?("\u0000")opts[:raw] = pg_clean_up(opts[:raw]) if opts[:raw] && opts[:raw].include?("\u0000")opts.delete(:reply_to_post_number) unless opts[:topic_id]+ opts[:visible] = false if opts[:visible].nil? && opts[:hidden_reason_id].present?@guardian = opts[:guardian] if opts[:guardian]@spam = false@@ -454,6 +456,12 @@ class PostCreatorpost.custom_fields = fieldsend+ if @opts[:hidden_reason_id].present?+ post.hidden = true+ post.hidden_at = Time.zone.now+ post.hidden_reason_id = @opts[:hidden_reason_id]+ end+@post = postenddiff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rbindex a6525afce8c..927f6961d5e 100644--- a/spec/components/email/receiver_spec.rb+++ b/spec/components/email/receiver_spec.rb@@ -622,6 +622,51 @@ describe Email::Receiver doexpect { process(:new_user) }.to change(Topic, :count)end+ it "creates visible topic for ham" do+ SiteSetting.email_in_spam_header = 'none'++ Fabricate(:user, email: "[email protected]", trust_level: SiteSetting.email_in_min_trust)+ expect { process(:existing_user) }.to change { Topic.count }.by(1) # Topic created++ topic = Topic.last+ expect(topic.visible).to eq(true)++ post = Post.last+ expect(post.hidden).to eq(false)+ expect(post.hidden_at).to eq(nil)+ expect(post.hidden_reason_id).to eq(nil)+ end++ it "creates hidden topic for X-Spam-Flag" do+ SiteSetting.email_in_spam_header = 'X-Spam-Flag'++ Fabricate(:user, email: "[email protected]", trust_level: SiteSetting.email_in_min_trust)+ expect { process(:spam_x_spam_flag) }.to change { Topic.count }.by(1) # Topic created++ topic = Topic.last+ expect(topic.visible).to eq(false)++ post = Post.last+ expect(post.hidden).to eq(true)+ expect(post.hidden_at).not_to eq(nil)+ expect(post.hidden_reason_id).to eq(Post.hidden_reasons[:email_spam_header_found])+ end++ it "creates hidden topic for X-Spam-Status" do+ SiteSetting.email_in_spam_header = 'X-Spam-Status'++ Fabricate(:user, email: "[email protected]", trust_level: SiteSetting.email_in_min_trust)+ expect { process(:spam_x_spam_status) }.to change { Topic.count }.by(1) # Topic created++ topic = Topic.last+ expect(topic.visible).to eq(false)++ post = Post.last+ expect(post.hidden).to eq(true)+ expect(post.hidden_at).not_to eq(nil)+ expect(post.hidden_reason_id).to eq(Post.hidden_reasons[:email_spam_header_found])+ end+it "adds the 'elided' part of the original message when always_show_trimmed_content is enabled" doSiteSetting.always_show_trimmed_content = truediff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rbindex 876b5a9830b..38ddc6ed74d 100644--- a/spec/components/post_creator_spec.rb+++ b/spec/components/post_creator_spec.rb@@ -38,6 +38,15 @@ describe PostCreator doexpect(post.wiki).to eq(true)end+ it "can be created with a hidden reason" do+ hri = Post.hidden_reasons[:flag_threshold_reached]+ post = PostCreator.create(user, basic_topic_params.merge(hidden_reason_id: hri))+ expect(post.hidden).to eq(true)+ expect(post.hidden_at).to be_present+ expect(post.hidden_reason_id).to eq(hri)+ expect(post.topic.visible).to eq(false)+ end+it "ensures the user can create the topic" doGuardian.any_instance.expects(:can_create?).with(Topic, nil).returns(false)expect { creator.create }.to raise_error(Discourse::InvalidAccess)@@ -71,6 +80,14 @@ describe PostCreator docontext "success" dobefore { creator }+ it "is not hidden" do+ p = creator.create+ expect(p.hidden).to eq(false)+ expect(p.hidden_at).not_to be_present+ expect(p.hidden_reason_id).to eq(nil)+ expect(p.topic.visible).to eq(true)+ end+it "doesn't return true for spam" docreator.createexpect(creator.spam?).to eq(false)diff --git a/spec/fixtures/emails/spam_x_spam_flag.eml b/spec/fixtures/emails/spam_x_spam_flag.emlnew file mode 100644index 00000000000..ae78762bf13--- /dev/null+++ b/spec/fixtures/emails/spam_x_spam_flag.eml@@ -0,0 +1,12 @@+Return-Path: <[email protected]>+From: Foo Bar <[email protected]>+To: [email protected]+Subject: This is a topic from an existing user+Date: Fri, 15 Jan 2016 00:12:43 +0100+Message-ID: <[email protected]>+Mime-Version: 1.0+Content-Type: text/plain; charset=UTF-8+Content-Transfer-Encoding: quoted-printable+X-Spam-Flag: YES++Hey, this is a topic from an existing user ;)diff --git a/spec/fixtures/emails/spam_x_spam_status.eml b/spec/fixtures/emails/spam_x_spam_status.emlnew file mode 100644index 00000000000..07527d61abe--- /dev/null+++ b/spec/fixtures/emails/spam_x_spam_status.eml@@ -0,0 +1,12 @@+Return-Path: <[email protected]>+From: Foo Bar <[email protected]>+To: [email protected]+Subject: This is a topic from an existing user+Date: Fri, 15 Jan 2016 00:12:43 +0100+Message-ID: <[email protected]>+Mime-Version: 1.0+Content-Type: text/plain; charset=UTF-8+Content-Transfer-Encoding: quoted-printable+X-Spam-Status: Yes, score=12.3 required=4.5++Hey, this is a topic from an existing user ;)