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-06-21 15:57:49 +08:00
|
|
|
every SiteSetting.pop3s_polling_period_mins.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-06-24 08:16:56 +08:00
|
|
|
rescue => e
|
|
|
|
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-24 08:46:22 +08:00
|
|
|
when Email::Receiver::EmailLogNotFound
|
|
|
|
message_template = :email_reject_reply_key
|
2014-06-24 08:16:56 +08:00
|
|
|
when ActiveRecord::Rollback
|
|
|
|
message_template = :email_reject_post_error
|
|
|
|
else
|
2014-06-24 08:48:51 +08:00
|
|
|
message_template = nil
|
2014-06-24 08:16:56 +08:00
|
|
|
end
|
2014-06-23 06:04:24 +08:00
|
|
|
|
2014-06-24 08:16:56 +08:00
|
|
|
if message_template
|
2014-06-26 08:01:23 +08:00
|
|
|
# inform the user about the rejection
|
|
|
|
message = Mail::Message.new(mail_string)
|
2014-06-28 03:03:07 +08:00
|
|
|
client_message = RejectionMailer.send_rejection(message.from, message.body, message.to, message_template)
|
2014-06-24 08:16:56 +08:00
|
|
|
Email::Sender.new(client_message, message_template).send
|
|
|
|
else
|
2014-07-01 06:17:05 +08:00
|
|
|
Discourse.handle_exception(e, { code: "unknown error for incoming email", mail: mail_string} )
|
2014-06-24 08:16:56 +08:00
|
|
|
end
|
2014-02-28 20:05:09 +08:00
|
|
|
ensure
|
|
|
|
mail.delete
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
def poll_pop3s
|
2014-06-02 20:59:10 +08:00
|
|
|
if !SiteSetting.pop3s_polling_insecure
|
|
|
|
Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
|
|
|
|
end
|
2013-06-14 06:11:10 +08: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-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
|
2014-07-01 06:17:05 +08:00
|
|
|
Discourse.handle_exception(e, { code: "signing in for incoming email" } )
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|