2015-12-10 15:23:54 -06:00
|
|
|
require "net/pop"
|
|
|
|
|
|
|
|
class POP3PollingEnabledSettingValidator
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def initialize(opts = {})
|
2015-12-10 15:23:54 -06:00
|
|
|
@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 04:25:49 -06:00
|
|
|
elsif !authentication_works?
|
2015-12-10 15:23:54 -06:00
|
|
|
I18n.t("site_settings.errors.pop3_polling_authentication_failed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def authentication_works?
|
|
|
|
@authentication_works ||= begin
|
|
|
|
pop3 = Net::POP3.new(SiteSetting.pop3_polling_host, SiteSetting.pop3_polling_port)
|
|
|
|
pop3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if SiteSetting.pop3_polling_ssl
|
|
|
|
pop3.auth_only(SiteSetting.pop3_polling_username, SiteSetting.pop3_polling_password)
|
|
|
|
rescue Net::POPAuthenticationError
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
2015-12-10 15:23:54 -06:00
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2015-12-10 15:23:54 -06:00
|
|
|
end
|