2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 09:50:49 -04:00
|
|
|
module Email
|
|
|
|
|
|
|
|
class Processor
|
2018-08-21 02:29:58 -04:00
|
|
|
attr_reader :receiver
|
2016-06-22 09:50:49 -04:00
|
|
|
|
2020-07-10 05:05:55 -04:00
|
|
|
def initialize(mail, opts = {})
|
2016-06-22 09:50:49 -04:00
|
|
|
@mail = mail
|
2020-07-10 05:05:55 -04:00
|
|
|
@opts = opts
|
2016-06-22 09:50:49 -04:00
|
|
|
end
|
|
|
|
|
2020-07-10 05:05:55 -04:00
|
|
|
def self.process!(mail, opts = {})
|
|
|
|
Email::Processor.new(mail, opts).process!
|
2016-06-22 09:50:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def process!
|
|
|
|
begin
|
2020-07-10 05:05:55 -04:00
|
|
|
@receiver = Email::Receiver.new(@mail, @opts)
|
2017-11-17 08:48:48 -05:00
|
|
|
@receiver.process!
|
2016-08-08 16:28:27 -04:00
|
|
|
rescue RateLimiter::LimitExceeded
|
2020-07-10 05:05:55 -04:00
|
|
|
@opts[:retry_on_rate_limit] ? Jobs.enqueue(:process_email, mail: @mail) : raise
|
2016-06-22 09:50:49 -04:00
|
|
|
rescue => e
|
2018-11-26 13:59:37 -05:00
|
|
|
return handle_bounce(e) if @receiver.is_bounce?
|
|
|
|
|
2016-06-22 09:50:49 -04:00
|
|
|
log_email_process_failure(@mail, e)
|
2017-11-17 08:48:48 -05:00
|
|
|
incoming_email = @receiver.try(:incoming_email)
|
2017-09-15 10:47:19 -04:00
|
|
|
rejection_message = handle_failure(@mail, e)
|
2016-06-28 10:42:05 -04:00
|
|
|
if rejection_message.present?
|
2016-06-22 09:50:49 -04:00
|
|
|
set_incoming_email_rejection_message(incoming_email, rejection_message.body.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-26 13:59:37 -05:00
|
|
|
def handle_bounce(e)
|
|
|
|
# never reply to bounced emails
|
|
|
|
log_email_process_failure(@mail, e)
|
|
|
|
set_incoming_email_rejection_message(@receiver.incoming_email, I18n.t("emails.incoming.errors.bounced_email_error"))
|
|
|
|
end
|
|
|
|
|
2017-09-15 10:47:19 -04:00
|
|
|
def handle_failure(mail_string, e)
|
2016-06-22 09:50:49 -04:00
|
|
|
message_template = case e
|
2017-09-15 11:22:51 -04:00
|
|
|
when Email::Receiver::NoSenderDetectedError then return nil
|
2018-05-23 04:04:45 -04:00
|
|
|
when Email::Receiver::FromReplyByAddressError then return nil
|
2016-06-22 09:50:49 -04:00
|
|
|
when Email::Receiver::EmptyEmailError then :email_reject_empty
|
|
|
|
when Email::Receiver::NoBodyDetectedError then :email_reject_empty
|
|
|
|
when Email::Receiver::UserNotFoundError then :email_reject_user_not_found
|
|
|
|
when Email::Receiver::ScreenedEmailError then :email_reject_screened_email
|
2017-10-03 05:23:18 -04:00
|
|
|
when Email::Receiver::EmailNotAllowed then :email_reject_not_allowed_email
|
2016-06-22 09:50:49 -04:00
|
|
|
when Email::Receiver::AutoGeneratedEmailError then :email_reject_auto_generated
|
|
|
|
when Email::Receiver::InactiveUserError then :email_reject_inactive_user
|
2017-11-10 12:18:08 -05:00
|
|
|
when Email::Receiver::SilencedUserError then :email_reject_silenced_user
|
2016-06-22 09:50:49 -04:00
|
|
|
when Email::Receiver::BadDestinationAddress then :email_reject_bad_destination_address
|
|
|
|
when Email::Receiver::StrangersNotAllowedError then :email_reject_strangers_not_allowed
|
|
|
|
when Email::Receiver::InsufficientTrustLevelError then :email_reject_insufficient_trust_level
|
|
|
|
when Email::Receiver::ReplyUserNotMatchingError then :email_reject_reply_user_not_matching
|
|
|
|
when Email::Receiver::TopicNotFoundError then :email_reject_topic_not_found
|
|
|
|
when Email::Receiver::TopicClosedError then :email_reject_topic_closed
|
2017-10-03 18:01:33 -04:00
|
|
|
when Email::Receiver::InvalidPost then :email_reject_invalid_post
|
2018-08-02 15:43:53 -04:00
|
|
|
when Email::Receiver::TooShortPost then :email_reject_post_too_short
|
2017-10-03 04:13:19 -04:00
|
|
|
when Email::Receiver::UnsubscribeNotAllowed then :email_reject_invalid_post
|
2016-06-22 09:50:49 -04:00
|
|
|
when ActiveRecord::Rollback then :email_reject_invalid_post
|
|
|
|
when Email::Receiver::InvalidPostAction then :email_reject_invalid_post_action
|
|
|
|
when Discourse::InvalidAccess then :email_reject_invalid_access
|
2018-05-09 12:51:01 -04:00
|
|
|
when Email::Receiver::OldDestinationError then :email_reject_old_destination
|
2019-06-02 17:49:05 -04:00
|
|
|
when Email::Receiver::ReplyNotAllowedError then :email_reject_reply_not_allowed
|
2020-05-14 10:04:58 -04:00
|
|
|
when Email::Receiver::ReplyToDigestError then :email_reject_reply_to_digest
|
2017-09-12 16:35:24 -04:00
|
|
|
else :email_reject_unrecognized_error
|
2016-06-22 09:50:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
template_args = {}
|
|
|
|
client_message = nil
|
|
|
|
|
|
|
|
# there might be more information available in the exception
|
|
|
|
if message_template == :email_reject_invalid_post && e.message.size > 6
|
|
|
|
message_template = :email_reject_invalid_post_specified
|
|
|
|
template_args[:post_error] = e.message
|
|
|
|
end
|
|
|
|
|
2018-08-02 15:43:53 -04:00
|
|
|
if message_template == :email_reject_post_too_short
|
|
|
|
template_args[:count] = SiteSetting.min_post_length
|
|
|
|
end
|
|
|
|
|
2017-07-21 13:24:31 -04:00
|
|
|
if message_template == :email_reject_unrecognized_error
|
|
|
|
msg = "Unrecognized error type (#{e.class}: #{e.message}) when processing incoming email"
|
|
|
|
msg += "\n\nBacktrace:\n#{e.backtrace.map { |l| " #{l}" }.join("\n")}"
|
|
|
|
msg += "\n\nMail:\n#{mail_string}"
|
|
|
|
|
|
|
|
Rails.logger.error(msg)
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:51:01 -04:00
|
|
|
if message_template == :email_reject_old_destination
|
|
|
|
template_args[:short_url] = e.message
|
2018-08-21 04:17:08 -04:00
|
|
|
template_args[:number_of_days] = SiteSetting.disallow_reply_by_email_after_days
|
2018-05-09 12:51:01 -04:00
|
|
|
end
|
|
|
|
|
2016-06-22 09:50:49 -04:00
|
|
|
if message_template
|
|
|
|
# inform the user about the rejection
|
|
|
|
message = Mail::Message.new(mail_string)
|
|
|
|
template_args[:former_title] = message.subject
|
|
|
|
template_args[:destination] = message.to
|
|
|
|
template_args[:site_name] = SiteSetting.title
|
|
|
|
|
|
|
|
client_message = RejectionMailer.send_rejection(message_template, message.from, template_args)
|
2016-06-28 10:42:05 -04:00
|
|
|
|
2016-07-15 07:18:55 -04:00
|
|
|
# only send one rejection email per day to the same email address
|
|
|
|
if can_send_rejection_email?(message.from, message_template)
|
2016-06-28 10:42:05 -04:00
|
|
|
Email::Sender.new(client_message, message_template).send
|
|
|
|
end
|
2016-06-22 09:50:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
client_message
|
|
|
|
end
|
|
|
|
|
2016-07-15 07:18:55 -04:00
|
|
|
def can_send_rejection_email?(email, type)
|
2017-11-17 08:48:48 -05:00
|
|
|
return false if @receiver.sent_to_mailinglist_mirror?
|
2017-07-21 13:24:31 -04:00
|
|
|
return true if type == :email_reject_unrecognized_error
|
|
|
|
|
2016-07-15 07:18:55 -04:00
|
|
|
key = "rejection_email:#{email}:#{type}:#{Date.today}"
|
2016-06-28 10:42:05 -04:00
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
if Discourse.redis.setnx(key, "1")
|
|
|
|
Discourse.redis.expire(key, 25.hours)
|
2016-06-28 10:42:05 -04:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 09:50:49 -04:00
|
|
|
def set_incoming_email_rejection_message(incoming_email, message)
|
2019-10-30 01:54:35 -04:00
|
|
|
if incoming_email
|
|
|
|
incoming_email.update!(
|
|
|
|
rejection_message: message,
|
|
|
|
raw: Email::Cleaner.new(incoming_email.raw, rejected: true).execute
|
|
|
|
)
|
|
|
|
end
|
2016-06-22 09:50:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_email_process_failure(mail_string, exception)
|
|
|
|
if SiteSetting.log_mail_processing_failures
|
|
|
|
Rails.logger.warn("Email can not be processed: #{exception}\n\n#{mail_string}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|