2013-06-13 18:11:10 -04:00
|
|
|
#
|
|
|
|
# Connects to a mailbox and checks for replies
|
|
|
|
#
|
|
|
|
require 'net/pop'
|
|
|
|
require_dependency 'email/receiver'
|
2014-02-24 01:01:37 -05:00
|
|
|
require_dependency 'email/sender'
|
|
|
|
require_dependency 'email/message_builder'
|
2013-06-13 18:11:10 -04:00
|
|
|
|
|
|
|
module Jobs
|
2013-08-07 13:25:05 -04:00
|
|
|
class PollMailbox < Jobs::Scheduled
|
2014-02-05 18:14:41 -05:00
|
|
|
every 5.minutes
|
2013-06-13 18:11:10 -04:00
|
|
|
sidekiq_options retry: false
|
2014-02-24 01:01:37 -05:00
|
|
|
include Email::BuildEmailHelper
|
2013-06-13 18:11:10 -04:00
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
if SiteSetting.pop3s_polling_enabled?
|
|
|
|
poll_pop3s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-28 07:05:09 -05:00
|
|
|
def handle_mail(mail)
|
|
|
|
begin
|
2014-03-07 11:28:55 -05:00
|
|
|
mail_string = mail.pop
|
|
|
|
Email::Receiver.new(mail_string).process
|
2014-06-23 20:16:56 -04:00
|
|
|
rescue => e
|
2014-02-28 07:05:09 -05:00
|
|
|
# inform the user about the rejection
|
2014-04-09 13:26:19 -04:00
|
|
|
message = Mail::Message.new(mail_string)
|
2014-06-23 20:16:56 -04:00
|
|
|
message_template = nil
|
|
|
|
case e
|
|
|
|
when Email::Receiver::UserNotSufficientTrustLevelError
|
|
|
|
message_template = :email_reject_trust_level
|
|
|
|
when Email::Receiver::UserNotFoundError
|
|
|
|
message_template = :email_reject_no_account
|
|
|
|
when Email::Receiver::EmptyEmailError
|
|
|
|
message_template = :email_reject_empty
|
|
|
|
when Email::Receiver::EmailUnparsableError
|
|
|
|
message_template = :email_reject_parsing
|
2014-06-23 20:46:22 -04:00
|
|
|
when Email::Receiver::EmailLogNotFound
|
|
|
|
message_template = :email_reject_reply_key
|
2014-06-23 20:16:56 -04:00
|
|
|
when ActiveRecord::Rollback
|
|
|
|
message_template = :email_reject_post_error
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2014-06-22 18:04:24 -04:00
|
|
|
|
2014-06-23 20:16:56 -04:00
|
|
|
if message_template
|
|
|
|
# Send message to the user
|
|
|
|
client_message = RejectionMailer.send_rejection(message.from, message.body, message_template.to_s, "#{e.message}\n\n#{e.backtrace.join("\n")}")
|
|
|
|
Email::Sender.new(client_message, message_template).send
|
|
|
|
else
|
|
|
|
Rails.logger.error e
|
|
|
|
|
|
|
|
# If not known type, inform admins about the error
|
|
|
|
data = { limit_once_per: false, message_params: { from: message.from, source: message.body, error: "#{e.message}\n\n#{e.backtrace.join("\n")}" }}
|
|
|
|
GroupMessage.create(Group[:admins].name, :email_error_notification, data)
|
|
|
|
end
|
2014-02-28 07:05:09 -05:00
|
|
|
ensure
|
|
|
|
mail.delete
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-13 18:11:10 -04:00
|
|
|
def poll_pop3s
|
2014-06-02 08:59:10 -04:00
|
|
|
if !SiteSetting.pop3s_polling_insecure
|
|
|
|
Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
|
|
|
|
end
|
2013-06-13 18:11:10 -04:00
|
|
|
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-07 11:28:55 -05:00
|
|
|
handle_mail(mail)
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
pop.finish
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
2014-04-09 13:26:19 -04: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-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|