2013-06-11 04:46:08 +08:00
|
|
|
#
|
|
|
|
# Handles an incoming message
|
|
|
|
#
|
|
|
|
|
|
|
|
module Email
|
|
|
|
class Receiver
|
|
|
|
|
|
|
|
def self.results
|
2013-06-14 06:11:10 +08:00
|
|
|
@results ||= Enum.new(:unprocessable, :missing, :processed)
|
2013-06-11 04:46:08 +08:00
|
|
|
end
|
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
attr_reader :body, :reply_key, :email_log
|
|
|
|
|
|
|
|
def initialize(raw)
|
|
|
|
@raw = raw
|
2013-06-11 04:46:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
2013-06-14 06:11:10 +08:00
|
|
|
return Email::Receiver.results[:unprocessable] if @raw.blank?
|
|
|
|
|
2013-06-20 00:14:01 +08:00
|
|
|
@message = Mail::Message.new(@raw)
|
2013-06-14 06:11:10 +08:00
|
|
|
|
2013-07-25 02:22:32 +08:00
|
|
|
|
|
|
|
# First remove the known discourse stuff.
|
|
|
|
parse_body
|
2013-06-14 06:11:10 +08:00
|
|
|
return Email::Receiver.results[:unprocessable] if @body.blank?
|
2013-06-11 04:46:08 +08:00
|
|
|
|
2013-07-25 02:22:32 +08:00
|
|
|
# Then run the github EmailReplyParser on it in case we didn't catch it
|
|
|
|
@body = EmailReplyParser.read(@body).visible_text
|
|
|
|
|
|
|
|
discourse_email_parser
|
|
|
|
|
|
|
|
return Email::Receiver.results[:unprocessable] if @body.blank?
|
2013-06-20 00:14:01 +08:00
|
|
|
@reply_key = @message.to.first
|
2013-06-14 06:11:10 +08:00
|
|
|
|
|
|
|
# Extract the `reply_key` from the format the site has specified
|
|
|
|
tokens = SiteSetting.reply_by_email_address.split("%{reply_key}")
|
|
|
|
tokens.each do |t|
|
|
|
|
@reply_key.gsub!(t, "") if t.present?
|
2013-06-11 04:46:08 +08:00
|
|
|
end
|
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
# Look up the email log for the reply key
|
|
|
|
@email_log = EmailLog.for(reply_key)
|
|
|
|
return Email::Receiver.results[:missing] if @email_log.blank?
|
|
|
|
|
|
|
|
create_reply
|
|
|
|
|
|
|
|
Email::Receiver.results[:processed]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-06-20 00:14:01 +08:00
|
|
|
def parse_body
|
2013-06-21 00:38:03 +08:00
|
|
|
html = nil
|
|
|
|
|
|
|
|
# If the message is multipart, find the best type for our purposes
|
|
|
|
if @message.multipart?
|
|
|
|
@message.parts.each do |p|
|
|
|
|
if p.content_type =~ /text\/plain/
|
2013-07-25 02:22:32 +08:00
|
|
|
@body = p.body.to_s
|
|
|
|
return @body
|
2013-06-21 00:38:03 +08:00
|
|
|
elsif p.content_type =~ /text\/html/
|
|
|
|
html = p.body.to_s
|
|
|
|
end
|
2013-06-20 00:14:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-21 00:38:03 +08:00
|
|
|
html = @message.body.to_s if @message.content_type =~ /text\/html/
|
|
|
|
if html.present?
|
2013-07-25 02:22:32 +08:00
|
|
|
@body = scrub_html(html)
|
|
|
|
return @body
|
2013-06-21 00:38:03 +08:00
|
|
|
end
|
|
|
|
|
2013-07-25 02:22:32 +08:00
|
|
|
@body = @message.body.to_s.strip
|
2013-06-21 00:38:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def scrub_html(html)
|
|
|
|
# If we have an HTML message, strip the markup
|
|
|
|
doc = Nokogiri::HTML(html)
|
|
|
|
|
|
|
|
# Blackberry is annoying in that it only provides HTML. We can easily
|
|
|
|
# extract it though
|
|
|
|
content = doc.at("#BB10_response_div")
|
|
|
|
return content.text if content.present?
|
|
|
|
|
|
|
|
return doc.xpath("//text()").text
|
2013-06-20 00:14:01 +08:00
|
|
|
end
|
2013-06-14 06:11:10 +08:00
|
|
|
|
2013-07-25 02:22:32 +08:00
|
|
|
def discourse_email_parser
|
2013-07-30 15:36:34 +08:00
|
|
|
lines = @body.lines.to_a
|
2013-07-25 02:22:32 +08:00
|
|
|
range_end = 0
|
|
|
|
|
|
|
|
email_year =
|
|
|
|
lines.each_with_index do |l, idx|
|
2013-06-14 06:11:10 +08:00
|
|
|
|
2013-07-25 02:22:32 +08:00
|
|
|
break if l =~ /\A\s*\-{3,80}\s*\z/ ||
|
|
|
|
l =~ Regexp.new("\\A\\s*" + I18n.t('user_notifications.previous_discussion') + "\\s*\\Z") ||
|
|
|
|
# This one might be controversial but so many reply lines have years, times and end with a colon.
|
|
|
|
# Let's try it and see how well it works.
|
|
|
|
(l =~ /\d{4}/ && l =~ /\d:\d\d/ && l =~ /\:$/)
|
|
|
|
|
|
|
|
range_end = idx
|
|
|
|
end
|
|
|
|
|
|
|
|
@body = lines[0..range_end].join
|
|
|
|
@body.strip!
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_reply
|
2013-06-14 06:11:10 +08:00
|
|
|
# Try to post the body as a reply
|
|
|
|
creator = PostCreator.new(email_log.user,
|
|
|
|
raw: @body,
|
|
|
|
topic_id: @email_log.topic_id,
|
2013-06-21 23:36:33 +08:00
|
|
|
reply_to_post_number: @email_log.post.post_number,
|
|
|
|
cooking_options: {traditional_markdown_linebreaks: true})
|
2013-06-11 04:46:08 +08:00
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
creator.create
|
2013-06-11 04:46:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|