2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 21:50:49 +08:00
|
|
|
module Email
|
|
|
|
|
|
|
|
class Processor
|
2018-08-21 14:29:58 +08:00
|
|
|
attr_reader :receiver
|
2016-06-22 21:50:49 +08:00
|
|
|
|
2020-07-10 17:05:55 +08:00
|
|
|
def initialize(mail, opts = {})
|
2016-06-22 21:50:49 +08:00
|
|
|
@mail = mail
|
2020-07-10 17:05:55 +08:00
|
|
|
@opts = opts
|
2016-06-22 21:50:49 +08:00
|
|
|
end
|
|
|
|
|
2020-07-10 17:05:55 +08:00
|
|
|
def self.process!(mail, opts = {})
|
|
|
|
Email::Processor.new(mail, opts).process!
|
2016-06-22 21:50:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def process!
|
|
|
|
begin
|
2020-07-10 17:05:55 +08:00
|
|
|
@receiver = Email::Receiver.new(@mail, @opts)
|
2017-11-17 21:48:48 +08:00
|
|
|
@receiver.process!
|
2016-08-09 04:28:27 +08:00
|
|
|
rescue RateLimiter::LimitExceeded
|
2020-07-10 17:05:55 +08:00
|
|
|
@opts[:retry_on_rate_limit] ? Jobs.enqueue(:process_email, mail: @mail) : raise
|
2016-06-22 21:50:49 +08:00
|
|
|
rescue => e
|
2018-11-27 02:59:37 +08:00
|
|
|
return handle_bounce(e) if @receiver.is_bounce?
|
|
|
|
|
2016-06-22 21:50:49 +08:00
|
|
|
log_email_process_failure(@mail, e)
|
2017-11-17 21:48:48 +08:00
|
|
|
incoming_email = @receiver.try(:incoming_email)
|
2017-09-15 22:47:19 +08:00
|
|
|
rejection_message = handle_failure(@mail, e)
|
2016-06-28 22:42:05 +08:00
|
|
|
if rejection_message.present?
|
2016-06-22 21:50:49 +08:00
|
|
|
set_incoming_email_rejection_message(incoming_email, rejection_message.body.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-27 02:59:37 +08: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 22:47:19 +08:00
|
|
|
def handle_failure(mail_string, e)
|
2016-06-22 21:50:49 +08:00
|
|
|
message_template = case e
|
2017-09-15 23:22:51 +08:00
|
|
|
when Email::Receiver::NoSenderDetectedError then return nil
|
2018-05-23 16:04:45 +08:00
|
|
|
when Email::Receiver::FromReplyByAddressError then return nil
|
2016-06-22 21:50:49 +08: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 17:23:18 +08:00
|
|
|
when Email::Receiver::EmailNotAllowed then :email_reject_not_allowed_email
|
2016-06-22 21:50:49 +08:00
|
|
|
when Email::Receiver::AutoGeneratedEmailError then :email_reject_auto_generated
|
|
|
|
when Email::Receiver::InactiveUserError then :email_reject_inactive_user
|
2017-11-11 01:18:08 +08:00
|
|
|
when Email::Receiver::SilencedUserError then :email_reject_silenced_user
|
2016-06-22 21:50:49 +08: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-04 06:01:33 +08:00
|
|
|
when Email::Receiver::InvalidPost then :email_reject_invalid_post
|
2018-08-03 03:43:53 +08:00
|
|
|
when Email::Receiver::TooShortPost then :email_reject_post_too_short
|
2017-10-03 16:13:19 +08:00
|
|
|
when Email::Receiver::UnsubscribeNotAllowed then :email_reject_invalid_post
|
2016-06-22 21:50:49 +08: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-10 00:51:01 +08:00
|
|
|
when Email::Receiver::OldDestinationError then :email_reject_old_destination
|
2019-06-03 05:49:05 +08:00
|
|
|
when Email::Receiver::ReplyNotAllowedError then :email_reject_reply_not_allowed
|
2020-05-14 22:04:58 +08:00
|
|
|
when Email::Receiver::ReplyToDigestError then :email_reject_reply_to_digest
|
2017-09-13 04:35:24 +08:00
|
|
|
else :email_reject_unrecognized_error
|
2016-06-22 21:50:49 +08: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-03 03:43:53 +08:00
|
|
|
if message_template == :email_reject_post_too_short
|
|
|
|
template_args[:count] = SiteSetting.min_post_length
|
|
|
|
end
|
|
|
|
|
2017-07-22 01:24:31 +08: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-10 00:51:01 +08:00
|
|
|
if message_template == :email_reject_old_destination
|
|
|
|
template_args[:short_url] = e.message
|
2018-08-21 16:17:08 +08:00
|
|
|
template_args[:number_of_days] = SiteSetting.disallow_reply_by_email_after_days
|
2018-05-10 00:51:01 +08:00
|
|
|
end
|
|
|
|
|
2016-06-22 21:50:49 +08: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 22:42:05 +08:00
|
|
|
|
2016-07-15 19:18:55 +08: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 22:42:05 +08:00
|
|
|
Email::Sender.new(client_message, message_template).send
|
|
|
|
end
|
2016-06-22 21:50:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
client_message
|
|
|
|
end
|
|
|
|
|
2016-07-15 19:18:55 +08:00
|
|
|
def can_send_rejection_email?(email, type)
|
2017-11-17 21:48:48 +08:00
|
|
|
return false if @receiver.sent_to_mailinglist_mirror?
|
2017-07-22 01:24:31 +08:00
|
|
|
return true if type == :email_reject_unrecognized_error
|
|
|
|
|
2016-07-15 19:18:55 +08:00
|
|
|
key = "rejection_email:#{email}:#{type}:#{Date.today}"
|
2016-06-28 22:42:05 +08:00
|
|
|
|
2019-12-03 17:05:53 +08:00
|
|
|
if Discourse.redis.setnx(key, "1")
|
|
|
|
Discourse.redis.expire(key, 25.hours)
|
2016-06-28 22:42:05 +08:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 21:50:49 +08:00
|
|
|
def set_incoming_email_rejection_message(incoming_email, message)
|
2019-10-30 13:54:35 +08:00
|
|
|
if incoming_email
|
|
|
|
incoming_email.update!(
|
|
|
|
rejection_message: message,
|
|
|
|
raw: Email::Cleaner.new(incoming_email.raw, rejected: true).execute
|
|
|
|
)
|
|
|
|
end
|
2016-06-22 21:50:49 +08: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
|