new alternative reply by email addresses

This commit is contained in:
Régis Hanol 2016-06-10 16:14:42 +02:00
parent 9e75b14535
commit dffe50a2e6
7 changed files with 42 additions and 10 deletions

View file

@ -1171,6 +1171,7 @@ en:
reply_by_email_enabled: "Enable replying to topics via email."
reply_by_email_address: "Template for reply by email incoming email address, for example: %{reply_key}@reply.example.com or replies+%{reply_key}@example.com"
alternative_reply_by_email_addresses: "List of alternative templates for reply by email incoming email addresses."
incoming_email_prefer_html: "Use the HTML instead of the text for incoming email. May cause unexcpeted formatting issues!"
disable_emails: "Prevent Discourse from sending any kind of emails"
@ -1363,6 +1364,7 @@ en:
invalid_string_min: "Must be at least %{min} characters."
invalid_string_max: "Must be no more than %{max} characters."
invalid_reply_by_email_address: "Value must contain '%{reply_key}' and be different from the notification email."
invalid_alternative_reply_by_email_addresses: "All values must contain '%{reply_key}' and be different from the notification email."
pop3_polling_host_is_empty: "You must set a 'pop3 polling host' before enabling POP3 polling."
pop3_polling_username_is_empty: "You must set a 'pop3 polling username' before enabling POP3 polling."
pop3_polling_password_is_empty: "You must set a 'pop3 polling password' before enabling POP3 polling."

View file

@ -534,7 +534,9 @@ email:
validator: "ReplyByEmailEnabledValidator"
reply_by_email_address:
default: ''
validator: "ReplyByEmailAddressValidator"
validator: "AlternativeReplyByEmailAddressesValidator"
alternative_reply_by_email_addresses:
default: ''
manual_polling_enabled:
default: false
pop3_polling_enabled:

View file

@ -327,15 +327,26 @@ module Email
# reply
match = reply_by_email_address_regex.match(address)
if match && match[1].present?
email_log = EmailLog.for(match[1])
return { type: :reply, obj: email_log } if email_log
if match && match.captures
match.captures.each do |c|
next if c.blank?
email_log = EmailLog.for(c)
return { type: :reply, obj: email_log } if email_log
end
end
end
def reply_by_email_address_regex
@reply_by_email_address_regex ||= Regexp.new Regexp.escape(SiteSetting.reply_by_email_address)
.gsub(Regexp.escape("%{reply_key}"), "([[:xdigit:]]{32})")
@reply_by_email_address_regex ||= begin
reply_addresses = [
SiteSetting.reply_by_email_address,
*SiteSetting.alternative_reply_by_email_addresses.split("|")
]
escaped_reply_addresses = reply_addresses.select { |a| a.present? }
.map { |a| Regexp.escape(a) }
.map { |a| a.gsub(Regexp.escape("%{reply_key}"), "([[:xdigit:]]{32})") }
Regexp.new(escaped_reply_addresses.join("|"))
end
end
def group_incoming_emails_regex

View file

@ -0,0 +1,16 @@
class AlternativeReplyByEmailAddressesValidator
def initialize(opts={})
@opts = opts
end
def valid_value?(val)
return true if val.blank?
validator = ReplyByEmailAddressValidator.new(@opts)
val.split("|").all? { |v| validator.valid_value?(v) }
end
def error_message
I18n.t('site_settings.errors.invalid_alternative_reply_by_email_addresses')
end
end

View file

@ -6,9 +6,9 @@ class ReplyByEmailAddressValidator
def valid_value?(val)
return true if val.blank?
!!(val =~ /@/i) &&
!!(val =~ /%{reply_key}/i) &&
val.gsub(/\+?%{reply_key}/i, "") != SiteSetting.notification_email
!!val["@"] &&
!!val["%{reply_key}"] &&
val.gsub(/\+?%{reply_key}/, "") != SiteSetting.notification_email
end
def error_message

View file

@ -6,6 +6,7 @@ describe Email::Receiver do
before do
SiteSetting.email_in = true
SiteSetting.reply_by_email_address = "reply+%{reply_key}@bar.com"
SiteSetting.alternative_reply_by_email_addresses = "alt+%{reply_key}@bar.com"
end
def process(email_name)

View file

@ -1,6 +1,6 @@
Return-Path: <discourse@bar.com>
From: Foo Bar <discourse@bar.com>
To: reply+4f97315cc828096c9cb34c6f1a0d6fe8@bar.com
To: alt+4f97315cc828096c9cb34c6f1a0d6fe8@bar.com
Date: Fri, 15 Jan 2016 00:12:43 +0100
Message-ID: <18@foo.bar.mail>
Mime-Version: 1.0