From c75d58ab215e95081769736ceb8f5a169ed52378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 18 May 2016 23:07:01 +0200 Subject: [PATCH] FEATURE: new 'ignore_by_title' site setting --- config/locales/server.en.yml | 1 + config/site_settings.yml | 3 +++ lib/email/receiver.rb | 8 +++++++- spec/components/email/receiver_spec.rb | 5 +++++ spec/fixtures/emails/ignored.eml | 11 +++++++++++ 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/emails/ignored.eml diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index be73c0b7fc1..0d49e026f18 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1178,6 +1178,7 @@ en: auto_generated_whitelist: "List of email addresses that won't be checked for auto-generated content." block_auto_generated_emails: "Block incoming emails identified as being auto generated." bounce_score_threshold: "Max score before we will stop emailing a user. Soft bounce adds 1, hard bounce adds 2, score reset 30 days after last bounce." + ignore_by_title: "Ignore incoming emails based on their title." manual_polling_enabled: "Push emails using the API for email replies." pop3_polling_enabled: "Poll via POP3 for email replies." diff --git a/config/site_settings.yml b/config/site_settings.yml index 6a73beaa8ec..704e874b68a 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -576,6 +576,9 @@ email: client: true default: 4 min: 1 + ignore_by_title: + type: list + default: '' files: diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 4debd83f3d8..bc2b98005f0 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -38,14 +38,20 @@ module Email end def process! + return if is_blacklisted? @from_email, @from_display_name = parse_from_field @incoming_email = find_or_create_incoming_email process_internal rescue => e - @incoming_email.update_columns(error: e.to_s) + @incoming_email.update_columns(error: e.to_s) if @incoming_email raise end + def is_blacklisted? + return false if SiteSetting.ignore_by_title.blank? + Regexp.new(SiteSetting.ignore_by_title) =~ @mail.subject + end + def find_or_create_incoming_email IncomingEmail.find_or_create_by(message_id: @message_id) do |ie| ie.raw = @raw_email diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index 0cff20991c1..f996028ec16 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -407,6 +407,11 @@ describe Email::Receiver do expect { process(:tl4_user) }.to change(Topic, :count) end + it "ignores by title" do + SiteSetting.ignore_by_title = "foo" + expect { process(:ignored) }.to_not change(Topic, :count) + end + end end diff --git a/spec/fixtures/emails/ignored.eml b/spec/fixtures/emails/ignored.eml new file mode 100644 index 00000000000..1eafc6a6d23 --- /dev/null +++ b/spec/fixtures/emails/ignored.eml @@ -0,0 +1,11 @@ +Return-Path: +From: Foo Bar +To: category@foo.com +Subject: This is a foo topic +Date: Fri, 15 Jan 2016 00:12:43 +0100 +Message-ID: <53@foo.bar.mail> +Mime-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: quoted-printable + +Hey, this is an incoming email that will be ignored ;)