2013-06-14 06:11:10 +08:00
|
|
|
#
|
|
|
|
# Connects to a mailbox and checks for replies
|
|
|
|
#
|
|
|
|
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-02-06 07:14:41 +08:00
|
|
|
every 5.minutes
|
2013-06-14 06:11:10 +08:00
|
|
|
sidekiq_options retry: false
|
2014-02-24 14:01:37 +08:00
|
|
|
include Email::BuildEmailHelper
|
2013-06-14 06:11:10 +08:00
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
if SiteSetting.pop3s_polling_enabled?
|
|
|
|
poll_pop3s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-28 20:05:09 +08:00
|
|
|
def handle_mail(mail)
|
|
|
|
begin
|
2014-03-08 00:28:55 +08:00
|
|
|
mail_string = mail.pop
|
|
|
|
Email::Receiver.new(mail_string).process
|
2014-03-19 09:25:29 +08:00
|
|
|
rescue Email::Receiver::UserNotSufficientTrustLevelError
|
2014-02-28 20:05:09 +08:00
|
|
|
# inform the user about the rejection
|
2014-04-10 01:26:19 +08:00
|
|
|
message = Mail::Message.new(mail_string)
|
|
|
|
client_message = RejectionMailer.send_trust_level(message.from, message.body)
|
|
|
|
Email::Sender.new(client_message, :email_reject_trust_level).send
|
2014-02-28 20:05:09 +08:00
|
|
|
rescue Email::Receiver::ProcessingError
|
|
|
|
# all other ProcessingErrors are ok to be dropped
|
|
|
|
rescue StandardError => e
|
2014-04-10 01:26:19 +08:00
|
|
|
# inform admins about the error
|
|
|
|
data = { limit_once_per: false, message_params: { source: mail, error: e }}
|
|
|
|
GroupMessage.create(Group[:admins].name, :email_error_notification, data)
|
2014-02-28 20:05:09 +08:00
|
|
|
ensure
|
|
|
|
mail.delete
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
def poll_pop3s
|
|
|
|
Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
|
|
|
|
Net::POP3.start(SiteSetting.pop3s_polling_host,
|
|
|
|
SiteSetting.pop3s_polling_port,
|
|
|
|
SiteSetting.pop3s_polling_username,
|
|
|
|
SiteSetting.pop3s_polling_password) do |pop|
|
|
|
|
unless pop.mails.empty?
|
|
|
|
pop.each do |mail|
|
2014-03-08 00:28:55 +08:00
|
|
|
handle_mail(mail)
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
end
|
2014-04-15 04:55:57 +08:00
|
|
|
pop.finish
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
2014-04-10 01:26:19 +08:00
|
|
|
rescue Net::POPAuthenticationError => e
|
|
|
|
# inform admins about the error (1 message per hour to prevent too much SPAM)
|
|
|
|
data = { limit_once_per: 1.hour, message_params: { error: e }}
|
|
|
|
GroupMessage.create(Group[:admins].name, :email_error_notification, data)
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|