2013-06-14 06:11:10 +08:00
|
|
|
require 'net/pop'
|
|
|
|
require_dependency 'email/receiver'
|
2014-02-24 14:01:37 +08:00
|
|
|
require_dependency 'email/sender'
|
|
|
|
require_dependency 'email/message_builder'
|
2013-06-14 06:11:10 +08:00
|
|
|
|
|
|
|
module Jobs
|
2013-08-08 01:25:05 +08:00
|
|
|
class PollMailbox < Jobs::Scheduled
|
2014-08-27 07:52:35 +08:00
|
|
|
every SiteSetting.pop3_polling_period_mins.minutes
|
2013-06-14 06:11:10 +08:00
|
|
|
sidekiq_options retry: false
|
2016-01-19 07:57:55 +08:00
|
|
|
|
2014-02-24 14:01:37 +08:00
|
|
|
include Email::BuildEmailHelper
|
2013-06-14 06:11:10 +08:00
|
|
|
|
|
|
|
def execute(args)
|
2014-07-18 04:22:46 +08:00
|
|
|
@args = args
|
2016-02-01 23:56:32 +08:00
|
|
|
poll_pop3 if should_poll?
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_poll?
|
|
|
|
return false if Rails.env.development? && ENV["POLL_MAILBOX"].nil?
|
|
|
|
SiteSetting.pop3_polling_enabled?
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
|
2016-01-19 07:57:55 +08:00
|
|
|
def process_popmail(popmail)
|
2014-02-28 20:05:09 +08:00
|
|
|
begin
|
2016-01-19 07:57:55 +08:00
|
|
|
mail_string = popmail.pop
|
2016-03-07 23:56:17 +08:00
|
|
|
receiver = Email::Receiver.new(mail_string)
|
|
|
|
receiver.process!
|
2014-06-24 08:16:56 +08:00
|
|
|
rescue => e
|
2016-03-07 23:56:17 +08:00
|
|
|
rejection_message = handle_failure(mail_string, e)
|
|
|
|
if rejection_message.present? && receiver && receiver.incoming_email
|
|
|
|
receiver.incoming_email.rejection_message = rejection_message.body.to_s
|
|
|
|
receiver.incoming_email.save
|
|
|
|
end
|
2014-08-02 02:03:55 +08:00
|
|
|
end
|
|
|
|
end
|
2014-08-02 00:56:15 +08:00
|
|
|
|
2014-08-02 02:03:55 +08:00
|
|
|
def handle_failure(mail_string, e)
|
2015-07-31 13:10:18 +08:00
|
|
|
Rails.logger.warn("Email can not be processed: #{e}\n\n#{mail_string}") if SiteSetting.log_mail_processing_failures
|
|
|
|
|
2016-01-19 07:57:55 +08:00
|
|
|
message_template = case e
|
|
|
|
when Email::Receiver::EmptyEmailError then :email_reject_empty
|
|
|
|
when Email::Receiver::NoBodyDetectedError then :email_reject_empty
|
2016-03-24 01:56:03 +08:00
|
|
|
when Email::Receiver::UserNotFoundError then :email_reject_user_not_found
|
2016-01-19 07:57:55 +08:00
|
|
|
when Email::Receiver::AutoGeneratedEmailError then :email_reject_auto_generated
|
|
|
|
when Email::Receiver::InactiveUserError then :email_reject_inactive_user
|
2016-02-11 17:39:57 +08:00
|
|
|
when Email::Receiver::BlockedUserError then :email_reject_blocked_user
|
2016-01-19 07:57:55 +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
|
|
|
|
when Email::Receiver::InvalidPost then :email_reject_invalid_post
|
|
|
|
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
|
2016-03-07 23:56:17 +08:00
|
|
|
when RateLimiter::LimitExceeded then :email_reject_rate_limit_specified
|
2016-01-19 07:57:55 +08:00
|
|
|
end
|
|
|
|
|
2014-08-02 02:03:55 +08:00
|
|
|
template_args = {}
|
2016-03-22 01:49:01 +08:00
|
|
|
client_message = nil
|
2016-01-19 07:57:55 +08:00
|
|
|
|
|
|
|
# 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
|
2014-08-02 02:03:55 +08:00
|
|
|
end
|
|
|
|
|
2016-03-07 23:56:17 +08:00
|
|
|
if message_template == :email_reject_rate_limit_specified
|
|
|
|
template_args[:rate_limit_description] = e.description
|
|
|
|
end
|
|
|
|
|
2014-08-02 02:03:55 +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
|
2015-04-01 13:50:02 +08:00
|
|
|
template_args[:site_name] = SiteSetting.title
|
2014-08-02 02:03:55 +08:00
|
|
|
|
|
|
|
client_message = RejectionMailer.send_rejection(message_template, message.from, template_args)
|
|
|
|
Email::Sender.new(client_message, message_template).send
|
|
|
|
else
|
2016-03-17 04:17:48 +08:00
|
|
|
mark_as_errored!
|
2015-02-10 04:47:46 +08:00
|
|
|
Discourse.handle_job_exception(e, error_context(@args, "Unrecognized error type when processing incoming email", mail: mail_string))
|
2014-02-28 20:05:09 +08:00
|
|
|
end
|
2016-03-22 01:49:01 +08:00
|
|
|
|
|
|
|
client_message
|
2014-02-28 20:05:09 +08:00
|
|
|
end
|
|
|
|
|
2014-08-27 07:52:35 +08:00
|
|
|
def poll_pop3
|
2016-01-19 07:57:55 +08:00
|
|
|
pop3 = Net::POP3.new(SiteSetting.pop3_polling_host, SiteSetting.pop3_polling_port)
|
|
|
|
pop3.enable_ssl if SiteSetting.pop3_polling_ssl
|
2014-08-27 08:00:27 +08:00
|
|
|
|
2016-01-19 07:57:55 +08:00
|
|
|
pop3.start(SiteSetting.pop3_polling_username, SiteSetting.pop3_polling_password) do |pop|
|
|
|
|
pop.delete_all do |p|
|
|
|
|
process_popmail(p)
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
end
|
2016-03-25 23:44:08 +08:00
|
|
|
rescue Net::OpenTimeout => e
|
|
|
|
mark_as_errored!
|
|
|
|
Discourse.handle_job_exception(e, error_context(@args, "Connecting to '#{SiteSetting.pop3_polling_host}' for polling emails."))
|
2014-04-10 01:26:19 +08:00
|
|
|
rescue Net::POPAuthenticationError => e
|
2016-03-17 04:17:48 +08:00
|
|
|
mark_as_errored!
|
2016-03-25 23:44:08 +08:00
|
|
|
Discourse.handle_job_exception(e, error_context(@args, "Signing in to poll incoming emails."))
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
|
2016-03-17 04:17:48 +08:00
|
|
|
POLL_MAILBOX_ERRORS_KEY ||= "poll_mailbox_errors".freeze
|
|
|
|
|
|
|
|
def self.errors_in_past_24_hours
|
|
|
|
$redis.zremrangebyscore(POLL_MAILBOX_ERRORS_KEY, 0, 24.hours.ago.to_i)
|
|
|
|
$redis.zcard(POLL_MAILBOX_ERRORS_KEY).to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_as_errored!
|
|
|
|
now = Time.now.to_i
|
|
|
|
$redis.zadd(POLL_MAILBOX_ERRORS_KEY, now, now.to_s)
|
|
|
|
end
|
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
end
|