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'
|
2015-12-30 14:04:05 -05:00
|
|
|
require_dependency 'post_action_creator'
|
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
|
2015-12-15 18:43:05 -05:00
|
|
|
class ReplyUserNotFoundError < ProcessingError; end
|
|
|
|
class ReplyUserNotMatchingError < ProcessingError; end
|
2015-12-21 11:54:02 -05:00
|
|
|
class InactiveUserError < ProcessingError; end
|
2015-12-30 06:17:45 -05:00
|
|
|
class InvalidPostAction < 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
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
@message = Mail.new(@raw)
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
raise AutoGeneratedEmailError if @message.header.to_s =~ /auto-(replied|generated)/
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
@body = parse_body(@message)
|
2014-08-13 14:06:17 -04:00
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
# 'smtp_envelope_to' is a combination of: to, cc and bcc fields
|
|
|
|
# prioriziting the `:reply` types
|
|
|
|
dest_infos = @message.smtp_envelope_to
|
|
|
|
.map { |to_address| check_address(to_address) }
|
|
|
|
.compact
|
|
|
|
.sort do |a, b|
|
|
|
|
if a[:type] == :reply && b[:type] != :reply
|
|
|
|
1
|
|
|
|
elsif a[:type] != :reply && b[:type] == :reply
|
|
|
|
-1
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
raise BadDestinationAddress if dest_infos.empty?
|
2014-08-26 20:31:51 -04:00
|
|
|
|
2015-12-10 17:52:20 -05:00
|
|
|
from = @message[:from].address_list.addresses.first
|
2015-12-18 11:54:42 -05:00
|
|
|
user_email = from.address
|
2015-12-10 17:52:20 -05:00
|
|
|
user_name = from.display_name
|
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
user = User.find_by_email(user_email)
|
2015-12-21 11:54:02 -05:00
|
|
|
raise InactiveUserError if user.present? && !user.active && !user.staged
|
2015-12-07 11:01:08 -05:00
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
# TODO: take advantage of all the "TO"s
|
|
|
|
dest_info = dest_infos[0]
|
2015-12-07 11:01:08 -05:00
|
|
|
case dest_info[:type]
|
|
|
|
when :group
|
|
|
|
group = dest_info[:obj]
|
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
if user.blank?
|
2015-12-07 11:01:08 -05:00
|
|
|
if SiteSetting.allow_staged_accounts
|
2015-12-15 18:43:05 -05:00
|
|
|
user = create_staged_account(user_email, user_name)
|
2015-12-07 11:01:08 -05:00
|
|
|
else
|
|
|
|
wrap_body_in_quote(user_email)
|
2015-12-15 18:43:05 -05:00
|
|
|
user = Discourse.system_user
|
2015-12-07 11:01:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
create_new_topic(user, archetype: Archetype.private_message, target_group_names: [group.name])
|
2015-12-07 11:01:08 -05:00
|
|
|
when :category
|
|
|
|
category = dest_info[:obj]
|
2014-08-13 14:06:17 -04:00
|
|
|
|
2015-12-15 18:43:05 -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-15 18:43:05 -05:00
|
|
|
user = create_staged_account(user_email)
|
2015-11-24 10:58:26 -05:00
|
|
|
else
|
|
|
|
wrap_body_in_quote(user_email)
|
2015-12-15 18:43:05 -05:00
|
|
|
user = Discourse.system_user
|
2015-11-24 10:58:26 -05:00
|
|
|
end
|
2014-02-27 10:36:33 -05:00
|
|
|
end
|
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
raise UserNotFoundError if user.blank?
|
|
|
|
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-15 18:43:05 -05:00
|
|
|
create_new_topic(user, category: category.id)
|
2015-12-07 11:01:08 -05:00
|
|
|
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-15 18:43:05 -05:00
|
|
|
raise EmailLogNotFound if @email_log.blank?
|
|
|
|
raise TopicNotFoundError if Topic.find_by_id(@email_log.topic_id).nil?
|
|
|
|
raise TopicClosedError if Topic.find_by_id(@email_log.topic_id).closed?
|
|
|
|
raise ReplyUserNotFoundError if user.blank?
|
|
|
|
raise ReplyUserNotMatchingError if @email_log.user_id != user.id
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2015-12-30 06:17:45 -05:00
|
|
|
if post_action_type = post_action_for(@body)
|
|
|
|
create_post_action(@email_log, post_action_type)
|
|
|
|
else
|
|
|
|
create_reply(@email_log)
|
|
|
|
end
|
2014-02-28 07:05:09 -05:00
|
|
|
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-10 17:52:20 -05:00
|
|
|
def create_staged_account(email, name=nil)
|
2015-12-07 11:01:08 -05:00
|
|
|
User.create(
|
|
|
|
email: email,
|
2015-12-10 17:52:20 -05:00
|
|
|
username: UserNameSuggester.suggest(name.presence || email),
|
|
|
|
name: name.presence || User.suggest_name(email),
|
|
|
|
staged: true,
|
2015-12-07 11:01:08 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-08-13 14:06:17 -04:00
|
|
|
def check_address(address)
|
2015-12-15 18:43:05 -05:00
|
|
|
# only check for a group/category when 'email_in' is enabled
|
2015-12-10 17:49:16 -05:00
|
|
|
if SiteSetting.email_in
|
|
|
|
group = Group.find_by_email(address)
|
2015-12-15 18:43:05 -05:00
|
|
|
return { address: address, type: :group, obj: group } if group
|
2015-12-07 11:01:08 -05:00
|
|
|
|
2015-12-10 17:49:16 -05:00
|
|
|
category = Category.find_by_email(address)
|
2015-12-15 18:43:05 -05:00
|
|
|
return { address: address, type: :category, obj: category } if category
|
2015-12-10 17:49:16 -05:00
|
|
|
end
|
2014-08-13 14:06:17 -04:00
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
match = reply_by_email_address_regex.match(address)
|
2014-08-13 14:06:17 -04:00
|
|
|
if match && match[1].present?
|
2015-12-15 18:43:05 -05:00
|
|
|
email_log = EmailLog.for(match[1])
|
|
|
|
return { address: address, type: :reply, obj: email_log }
|
2014-08-13 14:06:17 -04:00
|
|
|
end
|
2015-12-15 18:43:05 -05:00
|
|
|
end
|
2014-08-13 14:06:17 -04:00
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
def reply_by_email_address_regex
|
|
|
|
@reply_by_email_address_regex ||= Regexp.new Regexp.escape(SiteSetting.reply_by_email_address)
|
|
|
|
.gsub(Regexp.escape("%{reply_key}"), "([[:xdigit:]]{32})")
|
2014-08-13 14:06:17 -04:00
|
|
|
end
|
|
|
|
|
2014-08-26 20:31:51 -04:00
|
|
|
def parse_body(message)
|
2015-12-15 18:43:05 -05:00
|
|
|
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
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
body = discourse_email_trimmer(body)
|
2014-08-26 20:31:51 -04:00
|
|
|
raise EmptyEmailError if body.strip.blank?
|
2013-06-20 12:38:03 -04:00
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
body = DiscourseEmailParser.parse_reply(body)
|
2014-08-26 20:31:51 -04:00
|
|
|
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
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
REPLYING_HEADER_LABELS = ['From', 'Sent', 'To', 'Subject', 'In-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
|
|
|
|
|
2015-12-30 06:17:45 -05:00
|
|
|
def create_post_action(email_log, type)
|
2015-12-30 14:04:05 -05:00
|
|
|
PostActionCreator.new(email_log.user, email_log.post).perform(type)
|
2015-12-30 14:52:36 -05:00
|
|
|
rescue Discourse::InvalidAccess, PostAction::AlreadyActed => e
|
2015-12-30 06:17:45 -05:00
|
|
|
raise InvalidPostAction.new(e)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_action_for(body)
|
|
|
|
if ['+1', I18n.t('post_action_types.like.title').downcase].include? body.downcase
|
|
|
|
PostActionType.types[:like]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-15 18:43:05 -05:00
|
|
|
def create_reply(email_log)
|
|
|
|
create_post_with_attachments(email_log.user,
|
2014-08-26 20:30:12 -04:00
|
|
|
raw: @body,
|
2015-12-15 18:43:05 -05:00
|
|
|
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-15 18:43:05 -05:00
|
|
|
def create_new_topic(user, topic_options={})
|
2015-12-07 11:01:08 -05:00
|
|
|
topic_options[:raw] = @body
|
|
|
|
topic_options[:title] = @message.subject
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2015-12-15 18:43:05 -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",
|
2015-12-15 18:43:05 -05:00
|
|
|
to_address: user.email,
|
2015-04-28 13:04:34 -04:00
|
|
|
topic_id: topic_id,
|
2015-12-15 18:43:05 -05:00
|
|
|
user_id: user.id,
|
2014-04-14 16:55:57 -04:00
|
|
|
)
|
|
|
|
|
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
|