FEATURE: Create hidden posts for received spam emails (#6010)

* Add possibility to add hidden posts with PostCreator

* FEATURE: Create hidden posts for received spam emails

Spamchecker usually have 3 results: HAM, SPAM and PROBABLY_SPAM
SPAM gets usually directly rejected and needs no further handling.
HAM is good message and usually gets passed unmodified.
PROBABLY_SPAM gets an additional header to allow further processing.
This change addes processing capabilities for such headers and marks
new posts created as hidden when received via email.
This commit is contained in:
Patrick Gansterer
2018-07-05 11:07:46 +02:00
committed by Régis Hanol
parent 6e3a2197f8
commit 28dd7fb562
9 changed files with 126 additions and 5 deletions

View File

@@ -111,6 +111,8 @@ module Email
raise 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 Email
create_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 Email
destinations.each do |destination|
begin
process_destination(destination, user, body, elided)
process_destination(destination, user, body, elided, hidden_reason_id)
rescue => e
first_exception ||= e
else
@@ -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_body
text = nil
html = nil
@@ -541,7 +555,7 @@ module Email
nil
end
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 Email
case destination[:type]
when :group
group = destination[:obj]
create_group_post(group, user, body, elided)
create_group_post(group, user, body, elided, hidden_reason_id)
when :category
category = destination[:obj]
@@ -560,6 +574,7 @@ module Email
create_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 Email
create_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?)
end
end
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 Email
create_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 Email
create_topic(user: user,
raw: body,
elided: elided,
hidden_reason_id: hidden_reason_id,
title: subject,
archetype: Archetype.private_message,
target_group_names: [group.name],