2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-13 18:11:10 -04:00
|
|
|
require 'net/pop'
|
|
|
|
|
|
|
|
module Jobs
|
2019-10-02 00:01:53 -04:00
|
|
|
class PollMailbox < ::Jobs::Scheduled
|
2014-08-26 19:52:35 -04:00
|
|
|
every SiteSetting.pop3_polling_period_mins.minutes
|
2013-06-13 18:11:10 -04:00
|
|
|
sidekiq_options retry: false
|
2016-01-18 18:57:55 -05:00
|
|
|
|
2014-02-24 01:01:37 -05:00
|
|
|
include Email::BuildEmailHelper
|
2013-06-13 18:11:10 -04:00
|
|
|
|
|
|
|
def execute(args)
|
2014-07-17 16:22:46 -04:00
|
|
|
@args = args
|
2016-02-01 10:56:32 -05: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-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
|
2021-01-18 18:49:50 -05:00
|
|
|
def process_popmail(mail_string)
|
2021-01-19 22:22:41 -05:00
|
|
|
Email::Processor.process!(mail_string, source: :pop3_poll)
|
2014-02-28 07:05:09 -05:00
|
|
|
end
|
|
|
|
|
2020-08-31 22:12:26 -04:00
|
|
|
POLL_MAILBOX_TIMEOUT_ERROR_KEY = "poll_mailbox_timeout_error_key"
|
2016-04-06 02:59:48 -04:00
|
|
|
|
2014-08-26 19:52:35 -04:00
|
|
|
def poll_pop3
|
2016-01-18 18:57:55 -05:00
|
|
|
pop3 = Net::POP3.new(SiteSetting.pop3_polling_host, SiteSetting.pop3_polling_port)
|
2016-11-11 15:59:15 -05:00
|
|
|
|
|
|
|
if SiteSetting.pop3_polling_ssl
|
|
|
|
if SiteSetting.pop3_polling_openssl_verify
|
2019-09-11 12:43:02 -04:00
|
|
|
pop3.enable_ssl(max_version: OpenSSL::SSL::TLS1_2_VERSION)
|
2016-11-11 15:59:15 -05:00
|
|
|
else
|
|
|
|
pop3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
|
|
|
|
end
|
|
|
|
end
|
2014-08-26 20:00:27 -04:00
|
|
|
|
2016-01-18 18:57:55 -05:00
|
|
|
pop3.start(SiteSetting.pop3_polling_username, SiteSetting.pop3_polling_password) do |pop|
|
2017-12-14 10:03:48 -05:00
|
|
|
pop.each_mail do |p|
|
2021-01-18 18:49:50 -05:00
|
|
|
mail_string = p.pop
|
2021-04-19 04:27:29 -04:00
|
|
|
next if mail_too_old?(mail_string)
|
2021-01-18 18:49:50 -05:00
|
|
|
process_popmail(mail_string)
|
2017-12-14 10:03:48 -05:00
|
|
|
p.delete if SiteSetting.pop3_polling_delete_from_server?
|
2021-04-19 04:27:29 -04:00
|
|
|
rescue => e
|
|
|
|
Discourse.handle_job_exception(e, error_context(@args, "Failed to process incoming email."))
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
end
|
2016-03-25 11:44:08 -04:00
|
|
|
rescue Net::OpenTimeout => e
|
2019-12-03 04:05:53 -05:00
|
|
|
count = Discourse.redis.incr(POLL_MAILBOX_TIMEOUT_ERROR_KEY).to_i
|
2016-04-21 03:04:03 -04:00
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.expire(
|
2016-04-21 03:04:03 -04:00
|
|
|
POLL_MAILBOX_TIMEOUT_ERROR_KEY,
|
|
|
|
SiteSetting.pop3_polling_period_mins.minutes * 3
|
|
|
|
) if count == 1
|
2016-04-06 02:59:48 -04:00
|
|
|
|
|
|
|
if count > 3
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.del(POLL_MAILBOX_TIMEOUT_ERROR_KEY)
|
2016-04-06 02:59:48 -04:00
|
|
|
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-09 13:26:19 -04:00
|
|
|
rescue Net::POPAuthenticationError => e
|
2016-03-16 16:17:48 -04:00
|
|
|
mark_as_errored!
|
2016-04-06 02:59:48 -04:00
|
|
|
add_admin_dashboard_problem_message('dashboard.poll_pop3_auth_error')
|
2016-03-25 11:44:08 -04:00
|
|
|
Discourse.handle_job_exception(e, error_context(@args, "Signing in to poll incoming emails."))
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
|
2020-08-31 22:12:26 -04:00
|
|
|
POLL_MAILBOX_ERRORS_KEY = "poll_mailbox_errors"
|
2016-03-16 16:17:48 -04:00
|
|
|
|
|
|
|
def self.errors_in_past_24_hours
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.zremrangebyscore(POLL_MAILBOX_ERRORS_KEY, 0, 24.hours.ago.to_i)
|
|
|
|
Discourse.redis.zcard(POLL_MAILBOX_ERRORS_KEY).to_i
|
2016-03-16 16:17:48 -04:00
|
|
|
end
|
|
|
|
|
2021-01-18 18:49:50 -05:00
|
|
|
def mail_too_old?(mail_string)
|
|
|
|
mail = Mail.new(mail_string)
|
|
|
|
date_header = mail.header['Date']
|
|
|
|
return false if date_header.blank?
|
|
|
|
|
|
|
|
date = Time.parse(date_header.to_s)
|
|
|
|
date < 1.week.ago
|
|
|
|
end
|
|
|
|
|
2016-03-16 16:17:48 -04:00
|
|
|
def mark_as_errored!
|
|
|
|
now = Time.now.to_i
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.zadd(POLL_MAILBOX_ERRORS_KEY, now, now.to_s)
|
2016-03-16 16:17:48 -04:00
|
|
|
end
|
|
|
|
|
2016-04-06 02:59:48 -04: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-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
end
|