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-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
|
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
|
2014-08-26 19:52:35 -04:00
|
|
|
if SiteSetting.pop3_polling_enabled?
|
|
|
|
poll_pop3
|
2013-06-13 18:11:10 -04:00
|
|
|
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-08-01 14:03:55 -04:00
|
|
|
handle_failure(mail_string, e)
|
|
|
|
ensure
|
|
|
|
mail.delete
|
|
|
|
end
|
|
|
|
end
|
2014-08-01 12:56:15 -04:00
|
|
|
|
2014-08-01 14:03:55 -04:00
|
|
|
def handle_failure(mail_string, e)
|
|
|
|
template_args = {}
|
|
|
|
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
|
|
|
|
when Email::Receiver::EmailLogNotFound
|
|
|
|
message_template = :email_reject_reply_key
|
2014-08-13 14:47:21 -04:00
|
|
|
when Email::Receiver::BadDestinationAddress
|
|
|
|
message_template = :email_reject_destination
|
2014-10-27 02:58:31 -04:00
|
|
|
when Email::Receiver::TopicNotFoundError
|
|
|
|
message_template = :email_reject_topic_not_found
|
2014-10-25 10:36:59 -04:00
|
|
|
when Email::Receiver::TopicClosedError
|
|
|
|
message_template = :email_reject_topic_closed
|
2015-02-18 08:23:51 -05:00
|
|
|
when Email::Receiver::AutoGeneratedEmailError
|
|
|
|
message_template = :email_reject_auto_generated
|
2014-08-01 14:03:55 -04:00
|
|
|
when ActiveRecord::Rollback
|
|
|
|
message_template = :email_reject_post_error
|
|
|
|
when Email::Receiver::InvalidPost
|
|
|
|
if e.message.length < 6
|
|
|
|
message_template = :email_reject_post_error
|
2014-06-23 20:16:56 -04:00
|
|
|
else
|
2014-08-01 14:03:55 -04:00
|
|
|
message_template = :email_reject_post_error_specified
|
|
|
|
template_args[:post_error] = e.message
|
|
|
|
end
|
2014-08-01 12:56:15 -04:00
|
|
|
|
2014-06-23 20:16:56 -04:00
|
|
|
else
|
2014-08-01 14:03:55 -04:00
|
|
|
message_template = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if message_template
|
|
|
|
# inform the user about the rejection
|
|
|
|
message = Mail::Message.new(mail_string)
|
|
|
|
template_args[:former_title] = message.subject
|
|
|
|
template_args[:destination] = message.to
|
|
|
|
|
|
|
|
client_message = RejectionMailer.send_rejection(message_template, message.from, template_args)
|
|
|
|
Email::Sender.new(client_message, message_template).send
|
|
|
|
else
|
2015-02-09 15:47:46 -05:00
|
|
|
Discourse.handle_job_exception(e, error_context(@args, "Unrecognized error type when processing incoming email", mail: mail_string))
|
2014-02-28 07:05:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-26 19:52:35 -04:00
|
|
|
def poll_pop3
|
2014-08-26 20:00:27 -04:00
|
|
|
connection = Net::POP3.new(SiteSetting.pop3_polling_host, SiteSetting.pop3_polling_port)
|
|
|
|
connection.enable_ssl if SiteSetting.pop3_polling_ssl
|
|
|
|
|
|
|
|
connection.start(SiteSetting.pop3_polling_username, SiteSetting.pop3_polling_password) do |pop|
|
2013-06-13 18:11:10 -04:00
|
|
|
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
|
2015-02-09 15:47:46 -05:00
|
|
|
Discourse.handle_job_exception(e, error_context(@args, "Signing in to poll incoming email"))
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|