2013-06-10 16:46:08 -04:00
|
|
|
#
|
|
|
|
# Handles an incoming message
|
|
|
|
#
|
|
|
|
|
|
|
|
module Email
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
class Receiver
|
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
2014-02-28 07:05:09 -05:00
|
|
|
class ProcessingError < StandardError; end
|
|
|
|
class EmailUnparsableError < ProcessingError; end
|
|
|
|
class EmptyEmailError < ProcessingError; end
|
|
|
|
class UserNotFoundError < ProcessingError; end
|
|
|
|
class UserNotSufficientTrustLevelError < ProcessingError; end
|
|
|
|
class EmailLogNotFound < ProcessingError; end
|
2013-06-10 16:46:08 -04:00
|
|
|
|
2013-06-13 18:11:10 -04:00
|
|
|
attr_reader :body, :reply_key, :email_log
|
|
|
|
|
|
|
|
def initialize(raw)
|
|
|
|
@raw = raw
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
2014-02-28 07:05:09 -05:00
|
|
|
raise EmptyEmailError if @raw.blank?
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2014-03-28 09:57:12 -04:00
|
|
|
@message = Mail.new(@raw)
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2013-07-24 14:22:32 -04:00
|
|
|
# First remove the known discourse stuff.
|
|
|
|
parse_body
|
2014-02-28 07:05:09 -05:00
|
|
|
raise EmptyEmailError if @body.blank?
|
2013-06-10 16:46:08 -04:00
|
|
|
|
2013-07-24 14:22:32 -04:00
|
|
|
# Then run the github EmailReplyParser on it in case we didn't catch it
|
2013-11-04 15:30:24 -05:00
|
|
|
@body = EmailReplyParser.read(@body).visible_text.force_encoding('UTF-8')
|
2013-07-24 14:22:32 -04:00
|
|
|
|
|
|
|
discourse_email_parser
|
2014-02-28 07:05:09 -05:00
|
|
|
raise EmailUnparsableError if @body.blank?
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2014-02-27 07:44:21 -05:00
|
|
|
if is_in_email?
|
2014-02-24 11:36:53 -05:00
|
|
|
@user = User.find_by_email(@message.from.first)
|
2014-04-14 16:55:57 -04:00
|
|
|
if @user.blank? && @allow_strangers
|
2014-02-27 10:36:33 -05:00
|
|
|
wrap_body_in_quote
|
|
|
|
@user = Discourse.system_user
|
|
|
|
end
|
|
|
|
|
2014-02-28 07:05:09 -05:00
|
|
|
raise UserNotFoundError if @user.blank?
|
2014-07-11 16:30:08 -04:00
|
|
|
raise UserNotSufficientTrustLevelError.new @user unless @allow_strangers || @user.has_trust_level?(TrustLevel.levels[SiteSetting.email_in_min_trust.to_i])
|
2014-02-24 11:36:53 -05:00
|
|
|
|
|
|
|
create_new_topic
|
2014-02-28 07:05:09 -05:00
|
|
|
else
|
|
|
|
@reply_key = @message.to.first
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2014-02-28 07:05:09 -05: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?
|
|
|
|
end
|
2014-02-27 07:44:21 -05:00
|
|
|
|
2014-02-28 07:05:09 -05:00
|
|
|
# Look up the email log for the reply key
|
|
|
|
@email_log = EmailLog.for(reply_key)
|
|
|
|
raise EmailLogNotFound if @email_log.blank?
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2014-02-28 07:05:09 -05:00
|
|
|
create_reply
|
|
|
|
end
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-06-19 12:14:01 -04:00
|
|
|
def parse_body
|
2013-06-20 12:38:03 -04:00
|
|
|
html = nil
|
|
|
|
|
|
|
|
# If the message is multipart, find the best type for our purposes
|
|
|
|
if @message.multipart?
|
2014-03-28 09:57:12 -04:00
|
|
|
if p = @message.text_part
|
|
|
|
@body = p.charset ? p.body.decoded.force_encoding(p.charset).encode("UTF-8").to_s : p.body.to_s
|
|
|
|
return @body
|
|
|
|
elsif p = @message.html_part
|
|
|
|
html = p.charset ? p.body.decoded.force_encoding(p.charset).encode("UTF-8").to_s : p.body.to_s
|
2013-06-19 12:14:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-16 21:24:32 -05:00
|
|
|
if @message.content_type =~ /text\/html/
|
|
|
|
if defined? @message.charset
|
2014-04-14 16:55:57 -04:00
|
|
|
html = @message.body.decoded.force_encoding(@message.charset).encode("UTF-8").to_s
|
2014-01-16 21:24:32 -05:00
|
|
|
else
|
|
|
|
html = @message.body.to_s
|
|
|
|
end
|
|
|
|
end
|
2014-03-28 09:57:12 -04:00
|
|
|
|
2013-06-20 12:38:03 -04:00
|
|
|
if html.present?
|
2013-07-24 14:22:32 -04:00
|
|
|
@body = scrub_html(html)
|
|
|
|
return @body
|
2013-06-20 12:38:03 -04:00
|
|
|
end
|
|
|
|
|
2014-01-16 21:24:32 -05:00
|
|
|
@body = @message.charset ? @message.body.decoded.force_encoding(@message.charset).encode("UTF-8").to_s.strip : @message.body.to_s
|
2013-11-20 13:29:42 -05:00
|
|
|
|
|
|
|
# Certain trigger phrases that means we didn't parse correctly
|
|
|
|
@body = nil if @body =~ /Content\-Type\:/ ||
|
|
|
|
@body =~ /multipart\/alternative/ ||
|
|
|
|
@body =~ /text\/plain/
|
|
|
|
|
|
|
|
@body
|
2013-06-20 12:38:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def scrub_html(html)
|
|
|
|
# If we have an HTML message, strip the markup
|
|
|
|
doc = Nokogiri::HTML(html)
|
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
# Blackberry is annoying in that it only provides HTML. We can easily extract it though
|
2013-06-20 12:38:03 -04:00
|
|
|
content = doc.at("#BB10_response_div")
|
|
|
|
return content.text if content.present?
|
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
doc.xpath("//text()").text
|
2013-06-19 12:14:01 -04:00
|
|
|
end
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2013-07-24 14:22:32 -04:00
|
|
|
def discourse_email_parser
|
2013-12-29 22:05:25 -05:00
|
|
|
lines = @body.scrub.lines.to_a
|
2013-07-24 14:22:32 -04:00
|
|
|
range_end = 0
|
|
|
|
|
2013-12-29 22:05:25 -05:00
|
|
|
lines.each_with_index do |l, idx|
|
2013-07-24 14:22:32 -04:00
|
|
|
break if l =~ /\A\s*\-{3,80}\s*\z/ ||
|
|
|
|
l =~ Regexp.new("\\A\\s*" + I18n.t('user_notifications.previous_discussion') + "\\s*\\Z") ||
|
2013-08-21 16:54:01 -04:00
|
|
|
(l =~ /via #{SiteSetting.title}(.*)\:$/) ||
|
2013-07-24 14:22:32 -04:00
|
|
|
# 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
|
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
def is_in_email?
|
|
|
|
@allow_strangers = false
|
2014-07-11 16:30:08 -04:00
|
|
|
return false unless SiteSetting.email_in
|
2014-04-14 16:55:57 -04:00
|
|
|
|
|
|
|
category = Category.find_by_email(@message.to.first)
|
|
|
|
return false unless category
|
|
|
|
|
|
|
|
@category_id = category.id
|
|
|
|
@allow_strangers = category.email_in_allow_strangers
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def wrap_body_in_quote
|
|
|
|
@body = "[quote=\"#{@message.from.first}\"]
|
|
|
|
#{@body}
|
|
|
|
[/quote]"
|
|
|
|
end
|
|
|
|
|
2013-07-24 14:22:32 -04:00
|
|
|
def create_reply
|
2014-04-14 16:55:57 -04:00
|
|
|
create_post_with_attachments(email_log.user, @body, @email_log.topic_id, @email_log.post.post_number)
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
2014-02-24 11:36:53 -05:00
|
|
|
|
|
|
|
def create_new_topic
|
2014-04-14 16:55:57 -04:00
|
|
|
topic = TopicCreator.new(
|
|
|
|
@user,
|
|
|
|
Guardian.new(@user),
|
|
|
|
category: @category_id,
|
|
|
|
title: @message.subject,
|
|
|
|
).create
|
|
|
|
|
|
|
|
post = create_post_with_attachments(@user, @body, topic.id)
|
|
|
|
|
|
|
|
EmailLog.create(
|
|
|
|
email_type: "topic_via_incoming_email",
|
|
|
|
to_address: @message.to.first,
|
|
|
|
topic_id: topic.id,
|
|
|
|
user_id: @user.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
post
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_post_with_attachments(user, raw, topic_id, reply_to_post_number=nil)
|
|
|
|
options = {
|
|
|
|
raw: raw,
|
|
|
|
topic_id: topic_id,
|
|
|
|
cooking_options: { traditional_markdown_linebreaks: true },
|
|
|
|
}
|
|
|
|
options[:reply_to_post_number] = reply_to_post_number if reply_to_post_number
|
|
|
|
|
|
|
|
# deal with attachments
|
|
|
|
@message.attachments.each do |attachment|
|
|
|
|
tmp = Tempfile.new("discourse-email-attachment")
|
|
|
|
begin
|
|
|
|
# read attachment
|
|
|
|
File.open(tmp.path, "w+b") { |f| f.write attachment.body.decoded }
|
|
|
|
# create the upload for the user
|
|
|
|
upload = Upload.create_for(user.id, tmp, attachment.filename, File.size(tmp))
|
|
|
|
if upload && upload.errors.empty?
|
|
|
|
# TODO: should use the same code as the client to insert attachments
|
|
|
|
raw << "\n#{attachment_markdown(upload)}\n"
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
tmp.close!
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
create_post(user, options)
|
|
|
|
end
|
|
|
|
|
2014-04-14 18:04:13 -04:00
|
|
|
def attachment_markdown(upload)
|
|
|
|
if FileHelper.is_image?(upload.original_filename)
|
2014-04-14 16:55:57 -04:00
|
|
|
"<img src='#{upload.url}' width='#{upload.width}' height='#{upload.height}'>"
|
|
|
|
else
|
|
|
|
"<a class='attachment' href='#{upload.url}'>#{upload.original_filename}</a> (#{number_to_human_size(upload.filesize)})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_post(user, options)
|
|
|
|
PostCreator.new(user, options).create
|
2014-02-24 01:01:37 -05:00
|
|
|
end
|
2013-06-10 16:46:08 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|