2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-10 16:23:54 -05:00
|
|
|
require "net/pop"
|
|
|
|
|
|
|
|
class POP3PollingEnabledSettingValidator
|
|
|
|
|
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_value?(val)
|
|
|
|
# only validate when enabling polling
|
|
|
|
return true if val == "f"
|
|
|
|
# ensure we can authenticate
|
|
|
|
SiteSetting.pop3_polling_host.present? &&
|
|
|
|
SiteSetting.pop3_polling_username.present? &&
|
|
|
|
SiteSetting.pop3_polling_password.present? &&
|
|
|
|
authentication_works?
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
|
|
|
if SiteSetting.pop3_polling_host.blank?
|
|
|
|
I18n.t("site_settings.errors.pop3_polling_host_is_empty")
|
|
|
|
elsif SiteSetting.pop3_polling_username.blank?
|
|
|
|
I18n.t("site_settings.errors.pop3_polling_username_is_empty")
|
|
|
|
elsif SiteSetting.pop3_polling_password.blank?
|
|
|
|
I18n.t("site_settings.errors.pop3_polling_password_is_empty")
|
2016-02-17 05:25:49 -05:00
|
|
|
elsif !authentication_works?
|
2015-12-10 16:23:54 -05:00
|
|
|
I18n.t("site_settings.errors.pop3_polling_authentication_failed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def authentication_works?
|
|
|
|
@authentication_works ||= begin
|
2022-01-03 17:30:48 -05:00
|
|
|
EmailSettingsValidator.validate_pop3(
|
|
|
|
host: SiteSetting.pop3_polling_host,
|
|
|
|
port: SiteSetting.pop3_polling_port,
|
|
|
|
ssl: SiteSetting.pop3_polling_ssl,
|
|
|
|
username: SiteSetting.pop3_polling_username,
|
|
|
|
password: SiteSetting.pop3_polling_password,
|
|
|
|
openssl_verify: SiteSetting.pop3_polling_openssl_verify
|
|
|
|
)
|
|
|
|
rescue *EmailSettingsExceptionHandler::EXPECTED_EXCEPTIONS => err
|
2015-12-10 16:23:54 -05:00
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2015-12-10 16:23:54 -05:00
|
|
|
end
|