2013-06-14 06:11:10 +08:00
|
|
|
require 'net/pop'
|
|
|
|
require_dependency 'email/receiver'
|
2016-06-22 21:50:49 +08:00
|
|
|
require_dependency 'email/processor'
|
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)
|
2016-06-22 21:50:49 +08:00
|
|
|
Email::Processor.process!(popmail.pop)
|
2014-02-28 20:05:09 +08:00
|
|
|
end
|
|
|
|
|
2016-04-06 14:59:48 +08:00
|
|
|
POLL_MAILBOX_TIMEOUT_ERROR_KEY = "poll_mailbox_timeout_error_key".freeze
|
|
|
|
|
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)
|
2016-11-12 04:59:15 +08:00
|
|
|
|
|
|
|
if SiteSetting.pop3_polling_ssl
|
|
|
|
if SiteSetting.pop3_polling_openssl_verify
|
|
|
|
pop3.enable_ssl
|
|
|
|
else
|
|
|
|
pop3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
|
|
|
|
end
|
|
|
|
end
|
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|
|
2017-12-14 23:03:48 +08:00
|
|
|
pop.each_mail do |p|
|
2016-01-19 07:57:55 +08:00
|
|
|
process_popmail(p)
|
2017-12-14 23:03:48 +08:00
|
|
|
p.delete if SiteSetting.pop3_polling_delete_from_server?
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
end
|
2016-03-25 23:44:08 +08:00
|
|
|
rescue Net::OpenTimeout => e
|
2016-04-06 14:59:48 +08:00
|
|
|
count = $redis.incr(POLL_MAILBOX_TIMEOUT_ERROR_KEY).to_i
|
2016-04-21 15:04:03 +08:00
|
|
|
|
|
|
|
$redis.expire(
|
|
|
|
POLL_MAILBOX_TIMEOUT_ERROR_KEY,
|
|
|
|
SiteSetting.pop3_polling_period_mins.minutes * 3
|
|
|
|
) if count == 1
|
2016-04-06 14:59:48 +08:00
|
|
|
|
|
|
|
if count > 3
|
|
|
|
$redis.del(POLL_MAILBOX_TIMEOUT_ERROR_KEY)
|
|
|
|
mark_as_errored!
|
|
|
|
add_admin_dashboard_problem_message('dashboard.poll_pop3_timeout')
|
|
|
|
Discourse.handle_job_exception(e, error_context(@args, "Connecting to '#{SiteSetting.pop3_polling_host}' for polling emails."))
|
|
|
|
end
|
2014-04-10 01:26:19 +08:00
|
|
|
rescue Net::POPAuthenticationError => e
|
2016-03-17 04:17:48 +08:00
|
|
|
mark_as_errored!
|
2016-04-06 14:59:48 +08:00
|
|
|
add_admin_dashboard_problem_message('dashboard.poll_pop3_auth_error')
|
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
|
|
|
|
|
2016-04-06 14:59:48 +08:00
|
|
|
def add_admin_dashboard_problem_message(i18n_key)
|
|
|
|
AdminDashboardData.add_problem_message(
|
|
|
|
i18n_key,
|
|
|
|
SiteSetting.pop3_polling_period_mins.minutes + 5.minutes
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
end
|