2015-04-28 13:04:34 -04:00
|
|
|
require_dependency 'new_post_manager'
|
2015-12-07 11:01:08 -05:00
|
|
|
require_dependency 'email/html_cleaner'
|
2013-06-10 16:46:08 -04:00
|
|
|
|
|
|
|
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
|
2014-08-13 14:06:17 -04:00
|
|
|
class BadDestinationAddress < ProcessingError; end
|
2014-10-27 02:58:31 -04:00
|
|
|
class TopicNotFoundError < ProcessingError; end
|
2014-10-25 10:36:59 -04:00
|
|
|
class TopicClosedError < ProcessingError; end
|
2015-02-18 08:23:51 -05:00
|
|
|
class AutoGeneratedEmailError < ProcessingError; end
|
2014-02-28 07:05:09 -05:00
|
|
|
class EmailLogNotFound < ProcessingError; end
|
2014-07-31 04:46:02 -04:00
|
|
|
class InvalidPost < ProcessingError; end
|
2013-06-10 16:46:08 -04:00
|
|
|
|
2014-08-13 14:06:17 -04:00
|
|
|
attr_reader :body, :email_log
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2015-05-22 15:40:26 -04:00
|
|
|
def initialize(raw, opts=nil)
|
2013-06-13 18:11:10 -04:00
|
|
|
@raw = raw
|
2015-05-22 15:40:26 -04:00
|
|
|
@opts = opts || {}
|
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-08-26 20:31:51 -04:00
|
|
|
message = Mail.new(@raw)
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
body = parse_body(message)
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
dest_info = { type: :invalid, obj: nil }
|
2014-08-26 20:31:51 -04:00
|
|
|
message.to.each do |to_address|
|
2015-12-07 11:01:08 -05:00
|
|
|
dest_info = check_address(to_address)
|
|
|
|
break if dest_info[:type] != :invalid
|
2014-08-13 14:06:17 -04:00
|
|
|
end
|
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
raise BadDestinationAddress if dest_info[:type] == :invalid
|
2015-02-18 08:23:51 -05:00
|
|
|
raise AutoGeneratedEmailError if message.header.to_s =~ /auto-generated/ || message.header.to_s =~ /auto-replied/
|
2014-08-13 14:06:17 -04:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
# TODO get to a state where we can remove this
|
|
|
|
@message = message
|
|
|
|
@body = body
|
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
user_email = @message.from.first
|
|
|
|
@user = User.find_by_email(user_email)
|
|
|
|
|
|
|
|
case dest_info[:type]
|
|
|
|
when :group
|
2014-08-13 14:06:17 -04:00
|
|
|
raise BadDestinationAddress unless SiteSetting.email_in
|
2015-12-07 11:01:08 -05:00
|
|
|
group = dest_info[:obj]
|
|
|
|
|
|
|
|
if @user.blank?
|
|
|
|
if SiteSetting.allow_staged_accounts
|
|
|
|
@user = create_staged_account(user_email)
|
|
|
|
else
|
|
|
|
wrap_body_in_quote(user_email)
|
|
|
|
@user = Discourse.system_user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
raise UserNotFoundError if @user.blank?
|
|
|
|
raise UserNotSufficientTrustLevelError.new(@user) unless @user.has_trust_level?(TrustLevel[SiteSetting.email_in_min_trust.to_i])
|
2014-08-13 14:06:17 -04:00
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
create_new_topic(archetype: Archetype.private_message, target_group_names: [group.name])
|
|
|
|
when :category
|
|
|
|
raise BadDestinationAddress unless SiteSetting.email_in
|
|
|
|
category = dest_info[:obj]
|
2014-08-13 14:06:17 -04:00
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
if @user.blank? && category.email_in_allow_strangers
|
2015-11-24 10:58:26 -05:00
|
|
|
if SiteSetting.allow_staged_accounts
|
2015-12-07 11:01:08 -05:00
|
|
|
@user = create_staged_account(user_email)
|
2015-11-24 10:58:26 -05:00
|
|
|
else
|
|
|
|
wrap_body_in_quote(user_email)
|
|
|
|
@user = Discourse.system_user
|
|
|
|
end
|
2014-02-27 10:36:33 -05:00
|
|
|
end
|
|
|
|
|
2014-02-28 07:05:09 -05:00
|
|
|
raise UserNotFoundError if @user.blank?
|
2015-12-07 11:01:08 -05:00
|
|
|
raise UserNotSufficientTrustLevelError.new(@user) unless category.email_in_allow_strangers || @user.has_trust_level?(TrustLevel[SiteSetting.email_in_min_trust.to_i])
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
create_new_topic(category: category.id)
|
|
|
|
when :reply
|
2014-08-13 14:06:17 -04:00
|
|
|
@email_log = dest_info[:obj]
|
2014-02-27 07:44:21 -05:00
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
raise EmailLogNotFound if @email_log.blank?
|
2014-10-27 02:58:31 -04:00
|
|
|
raise TopicNotFoundError if Topic.find_by_id(@email_log.topic_id).nil?
|
2015-12-07 11:01:08 -05:00
|
|
|
raise TopicClosedError if Topic.find_by_id(@email_log.topic_id).closed?
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2014-02-28 07:05:09 -05:00
|
|
|
create_reply
|
|
|
|
end
|
2015-12-07 11:01:08 -05:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError => e
|
|
|
|
raise EmailUnparsableError.new(e)
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
def create_staged_account(email)
|
|
|
|
User.create(
|
|
|
|
email: email,
|
|
|
|
username: UserNameSuggester.suggest(email),
|
|
|
|
name: User.suggest_name(email),
|
|
|
|
staged: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-08-13 14:06:17 -04:00
|
|
|
def check_address(address)
|
2015-12-07 11:01:08 -05:00
|
|
|
group = Group.find_by_email(address)
|
|
|
|
return { type: :group, obj: group } if group
|
|
|
|
|
2014-08-13 14:06:17 -04:00
|
|
|
category = Category.find_by_email(address)
|
2015-12-07 11:01:08 -05:00
|
|
|
return { type: :category, obj: category } if category
|
2014-08-13 14:06:17 -04:00
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
regex = Regexp.escape(SiteSetting.reply_by_email_address)
|
2014-08-13 14:06:17 -04:00
|
|
|
regex = regex.gsub(Regexp.escape('%{reply_key}'), "(.*)")
|
2015-12-07 11:01:08 -05:00
|
|
|
regex = Regexp.new(regex)
|
2014-08-13 14:06:17 -04:00
|
|
|
match = regex.match address
|
|
|
|
if match && match[1].present?
|
|
|
|
reply_key = match[1]
|
|
|
|
email_log = EmailLog.for(reply_key)
|
2015-12-07 11:01:08 -05:00
|
|
|
return { type: :reply, obj: email_log }
|
2014-08-13 14:06:17 -04:00
|
|
|
end
|
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
{ type: :invalid, obj: nil }
|
2014-08-13 14:06:17 -04:00
|
|
|
end
|
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
def parse_body(message)
|
|
|
|
body = select_body message
|
2014-08-28 15:09:42 -04:00
|
|
|
encoding = body.encoding
|
2014-08-26 20:31:51 -04:00
|
|
|
raise EmptyEmailError if body.strip.blank?
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
body = discourse_email_trimmer body
|
|
|
|
raise EmptyEmailError if body.strip.blank?
|
2013-06-20 12:38:03 -04:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
body = EmailReplyParser.parse_reply body
|
|
|
|
raise EmptyEmailError if body.strip.blank?
|
|
|
|
|
2014-08-28 15:09:42 -04:00
|
|
|
body.force_encoding(encoding).encode("UTF-8")
|
2014-08-26 20:31:51 -04:00
|
|
|
end
|
2013-06-19 12:14:01 -04:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
def select_body(message)
|
|
|
|
html = nil
|
2015-11-30 12:33:24 -05:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
if message.multipart?
|
|
|
|
text = fix_charset message.text_part
|
2015-11-30 12:33:24 -05:00
|
|
|
# prefer text over html
|
|
|
|
return text if text
|
|
|
|
html = fix_charset message.html_part
|
2014-08-26 20:31:51 -04:00
|
|
|
elsif message.content_type =~ /text\/html/
|
|
|
|
html = fix_charset message
|
2014-01-16 21:24:32 -05:00
|
|
|
end
|
2014-03-28 09:57:12 -04:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
if html
|
|
|
|
body = HtmlCleaner.new(html).output_html
|
|
|
|
else
|
|
|
|
body = fix_charset message
|
2013-06-20 12:38:03 -04:00
|
|
|
end
|
|
|
|
|
2015-05-22 15:40:26 -04:00
|
|
|
return body if @opts[:skip_sanity_check]
|
|
|
|
|
2013-11-20 13:29:42 -05:00
|
|
|
# Certain trigger phrases that means we didn't parse correctly
|
2014-08-26 20:31:51 -04:00
|
|
|
if body =~ /Content\-Type\:/ || body =~ /multipart\/alternative/ || body =~ /text\/plain/
|
|
|
|
raise EmptyEmailError
|
|
|
|
end
|
2013-11-20 13:29:42 -05:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
body
|
2013-06-20 12:38:03 -04:00
|
|
|
end
|
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
# Force encoding to UTF-8 on a Mail::Message or Mail::Part
|
|
|
|
def fix_charset(object)
|
|
|
|
return nil if object.nil?
|
2013-06-20 12:38:03 -04:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
if object.charset
|
2015-01-19 04:51:39 -05:00
|
|
|
object.body.decoded.force_encoding(object.charset.gsub(/utf8/i, "UTF-8")).encode("UTF-8").to_s
|
2014-08-26 20:31:51 -04:00
|
|
|
else
|
|
|
|
object.body.to_s
|
|
|
|
end
|
2015-07-23 18:37:40 -04:00
|
|
|
rescue
|
|
|
|
nil
|
2013-06-19 12:14:01 -04:00
|
|
|
end
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2014-10-22 15:22:04 -04:00
|
|
|
REPLYING_HEADER_LABELS = ['From', 'Sent', 'To', 'Subject', 'Reply To', 'Cc', 'Bcc', 'Date']
|
2014-08-26 20:31:51 -04:00
|
|
|
REPLYING_HEADER_REGEX = Regexp.union(REPLYING_HEADER_LABELS.map { |lbl| "#{lbl}:" })
|
|
|
|
|
2015-11-18 15:22:50 -05:00
|
|
|
def line_is_quote?(l)
|
|
|
|
l =~ /\A\s*\-{3,80}\s*\z/ ||
|
|
|
|
l =~ Regexp.new("\\A\\s*" + I18n.t('user_notifications.previous_discussion') + "\\s*\\Z") ||
|
|
|
|
(l =~ /via #{SiteSetting.title}(.*)\:$/) ||
|
|
|
|
# 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 =~ /\:$/) ||
|
|
|
|
(l =~ /On [\w, ]+\d+.*wrote:/)
|
|
|
|
end
|
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
def discourse_email_trimmer(body)
|
|
|
|
lines = body.scrub.lines.to_a
|
2015-11-18 15:22:50 -05:00
|
|
|
range_start = 0
|
2013-07-24 14:22:32 -04:00
|
|
|
range_end = 0
|
|
|
|
|
2015-11-18 15:22:50 -05:00
|
|
|
# If we started with a quote, skip it
|
2013-12-29 22:05:25 -05:00
|
|
|
lines.each_with_index do |l, idx|
|
2015-11-18 15:22:50 -05:00
|
|
|
break unless line_is_quote?(l) or l =~ /^>/ or l.blank?
|
|
|
|
range_start = idx + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
lines[range_start..-1].each_with_index do |l, idx|
|
|
|
|
break if line_is_quote?(l)
|
2013-07-24 14:22:32 -04:00
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
# Headers on subsequent lines
|
|
|
|
break if (0..2).all? { |off| lines[idx+off] =~ REPLYING_HEADER_REGEX }
|
|
|
|
# Headers on the same line
|
|
|
|
break if REPLYING_HEADER_LABELS.count { |lbl| l.include? lbl } >= 3
|
2015-11-18 15:22:50 -05:00
|
|
|
range_end = range_start + idx
|
2013-07-24 14:22:32 -04:00
|
|
|
end
|
|
|
|
|
2015-11-18 15:22:50 -05:00
|
|
|
lines[range_start..range_end].join.strip
|
2013-07-24 14:22:32 -04:00
|
|
|
end
|
|
|
|
|
2015-11-24 10:58:26 -05:00
|
|
|
private
|
|
|
|
|
2014-08-13 14:06:17 -04:00
|
|
|
def wrap_body_in_quote(user_email)
|
2015-11-24 10:58:26 -05:00
|
|
|
@body = "[quote=\"#{user_email}\"]\n#{@body}\n[/quote]"
|
2014-04-14 16:55:57 -04:00
|
|
|
end
|
|
|
|
|
2013-07-24 14:22:32 -04:00
|
|
|
def create_reply
|
2014-08-26 20:30:12 -04:00
|
|
|
create_post_with_attachments(@email_log.user,
|
|
|
|
raw: @body,
|
|
|
|
topic_id: @email_log.topic_id,
|
|
|
|
reply_to_post_number: @email_log.post.post_number)
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
def create_new_topic(topic_options={})
|
|
|
|
topic_options[:raw] = @body
|
|
|
|
topic_options[:title] = @message.subject
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
result = create_post_with_attachments(@user, topic_options)
|
2015-04-28 13:04:34 -04:00
|
|
|
topic_id = result.post.present? ? result.post.topic_id : nil
|
2015-12-07 11:01:08 -05:00
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
EmailLog.create(
|
|
|
|
email_type: "topic_via_incoming_email",
|
2014-08-26 20:30:12 -04:00
|
|
|
to_address: @message.from.first, # pick from address because we want the user's email
|
2015-04-28 13:04:34 -04:00
|
|
|
topic_id: topic_id,
|
2014-04-14 16:55:57 -04:00
|
|
|
user_id: @user.id,
|
|
|
|
)
|
|
|
|
|
2015-04-28 13:04:34 -04:00
|
|
|
result
|
2014-04-14 16:55:57 -04:00
|
|
|
end
|
|
|
|
|
2015-12-07 11:01:08 -05:00
|
|
|
def create_post_with_attachments(user, post_options={})
|
2014-04-14 16:55:57 -04:00
|
|
|
options = {
|
|
|
|
cooking_options: { traditional_markdown_linebreaks: true },
|
2015-12-07 11:01:08 -05:00
|
|
|
}.merge(post_options)
|
2014-08-26 20:30:12 -04:00
|
|
|
|
|
|
|
raw = options[:raw]
|
2014-04-14 16:55:57 -04:00
|
|
|
|
|
|
|
# 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
|
2015-02-03 12:44:18 -05:00
|
|
|
upload = Upload.create_for(user.id, tmp, attachment.filename, tmp.size)
|
2014-04-14 16:55:57 -04:00
|
|
|
if upload && upload.errors.empty?
|
2015-11-30 12:33:24 -05:00
|
|
|
# try to inline images
|
|
|
|
if attachment.content_type.start_with?("image/")
|
|
|
|
if raw =~ /\[image: Inline image \d+\]/
|
|
|
|
raw.sub!(/\[image: Inline image \d+\]/, attachment_markdown(upload))
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
raw << "\n#{attachment_markdown(upload)}\n"
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
tmp.close!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-26 20:30:12 -04:00
|
|
|
options[:raw] = raw
|
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
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)
|
2014-09-04 13:04:22 -04:00
|
|
|
# Mark the reply as incoming via email
|
|
|
|
options[:via_email] = true
|
2014-10-16 02:08:02 -04:00
|
|
|
options[:raw_email] = @raw
|
2014-09-04 13:04:22 -04:00
|
|
|
|
2015-04-28 13:04:34 -04:00
|
|
|
manager = NewPostManager.new(user, options)
|
|
|
|
result = manager.perform
|
2014-08-26 20:30:12 -04:00
|
|
|
|
2015-04-28 13:04:34 -04:00
|
|
|
if result.errors.present?
|
|
|
|
raise InvalidPost, result.errors.full_messages.join("\n")
|
2014-07-31 04:46:02 -04:00
|
|
|
end
|
2014-08-26 20:30:12 -04:00
|
|
|
|
2015-04-28 13:04:34 -04:00
|
|
|
result
|
2014-02-24 01:01:37 -05:00
|
|
|
end
|
2013-06-10 16:46:08 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|