2015-04-29 01:04:34 +08:00
|
|
|
require_dependency 'new_post_manager'
|
2014-08-27 08:31:51 +08:00
|
|
|
require 'email/html_cleaner'
|
2013-06-11 04:46:08 +08:00
|
|
|
#
|
|
|
|
# Handles an incoming message
|
|
|
|
#
|
|
|
|
|
|
|
|
module Email
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2013-06-11 04:46:08 +08:00
|
|
|
class Receiver
|
|
|
|
|
2014-04-15 04:55:57 +08:00
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
2014-02-28 20:05:09 +08:00
|
|
|
class ProcessingError < StandardError; end
|
|
|
|
class EmailUnparsableError < ProcessingError; end
|
|
|
|
class EmptyEmailError < ProcessingError; end
|
|
|
|
class UserNotFoundError < ProcessingError; end
|
|
|
|
class UserNotSufficientTrustLevelError < ProcessingError; end
|
2014-08-14 02:06:17 +08:00
|
|
|
class BadDestinationAddress < ProcessingError; end
|
2014-10-27 14:58:31 +08:00
|
|
|
class TopicNotFoundError < ProcessingError; end
|
2014-10-25 22:36:59 +08:00
|
|
|
class TopicClosedError < ProcessingError; end
|
2015-02-18 21:23:51 +08:00
|
|
|
class AutoGeneratedEmailError < ProcessingError; end
|
2014-02-28 20:05:09 +08:00
|
|
|
class EmailLogNotFound < ProcessingError; end
|
2014-07-31 16:46:02 +08:00
|
|
|
class InvalidPost < ProcessingError; end
|
2013-06-11 04:46:08 +08:00
|
|
|
|
2014-08-14 02:06:17 +08:00
|
|
|
attr_reader :body, :email_log
|
2013-06-14 06:11:10 +08:00
|
|
|
|
2015-05-23 03:40:26 +08:00
|
|
|
def initialize(raw, opts=nil)
|
2013-06-14 06:11:10 +08:00
|
|
|
@raw = raw
|
2015-05-23 03:40:26 +08:00
|
|
|
@opts = opts || {}
|
2013-06-11 04:46:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
2014-02-28 20:05:09 +08:00
|
|
|
raise EmptyEmailError if @raw.blank?
|
2013-06-14 06:11:10 +08:00
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
message = Mail.new(@raw)
|
2013-06-14 06:11:10 +08:00
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
body = parse_body message
|
2014-02-25 00:36:53 +08:00
|
|
|
|
2014-08-14 02:06:17 +08:00
|
|
|
dest_info = {type: :invalid, obj: nil}
|
2014-08-27 08:31:51 +08:00
|
|
|
message.to.each do |to_address|
|
2014-08-14 02:06:17 +08:00
|
|
|
if dest_info[:type] == :invalid
|
|
|
|
dest_info = check_address to_address
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
raise BadDestinationAddress if dest_info[:type] == :invalid
|
2015-02-18 21:23:51 +08:00
|
|
|
raise AutoGeneratedEmailError if message.header.to_s =~ /auto-generated/ || message.header.to_s =~ /auto-replied/
|
2014-08-14 02:06:17 +08:00
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
# TODO get to a state where we can remove this
|
|
|
|
@message = message
|
|
|
|
@body = body
|
|
|
|
|
2014-08-14 02:06:17 +08:00
|
|
|
if dest_info[:type] == :category
|
|
|
|
raise BadDestinationAddress unless SiteSetting.email_in
|
|
|
|
category = dest_info[:obj]
|
|
|
|
@category_id = category.id
|
|
|
|
@allow_strangers = category.email_in_allow_strangers
|
|
|
|
|
|
|
|
user_email = @message.from.first
|
|
|
|
@user = User.find_by_email(user_email)
|
2014-04-15 04:55:57 +08:00
|
|
|
if @user.blank? && @allow_strangers
|
2014-08-14 02:06:17 +08:00
|
|
|
|
|
|
|
wrap_body_in_quote user_email
|
2014-07-31 16:46:02 +08:00
|
|
|
# TODO This is WRONG it should register an account
|
|
|
|
# and email the user details on how to log in / activate
|
2014-02-27 23:36:33 +08:00
|
|
|
@user = Discourse.system_user
|
|
|
|
end
|
|
|
|
|
2014-02-28 20:05:09 +08:00
|
|
|
raise UserNotFoundError if @user.blank?
|
2014-09-05 13:20:39 +08:00
|
|
|
raise UserNotSufficientTrustLevelError.new @user unless @allow_strangers || @user.has_trust_level?(TrustLevel[SiteSetting.email_in_min_trust.to_i])
|
2014-02-25 00:36:53 +08:00
|
|
|
|
|
|
|
create_new_topic
|
2014-02-28 20:05:09 +08:00
|
|
|
else
|
2014-08-14 02:06:17 +08:00
|
|
|
@email_log = dest_info[:obj]
|
2014-02-27 20:44:21 +08:00
|
|
|
|
2014-02-28 20:05:09 +08:00
|
|
|
raise EmailLogNotFound if @email_log.blank?
|
2014-10-27 14:58:31 +08:00
|
|
|
raise TopicNotFoundError if Topic.find_by_id(@email_log.topic_id).nil?
|
2014-10-25 22:36:59 +08:00
|
|
|
raise TopicClosedError if Topic.find_by_id(@email_log.topic_id).closed?
|
2014-02-25 00:36:53 +08:00
|
|
|
|
2014-02-28 20:05:09 +08:00
|
|
|
create_reply
|
|
|
|
end
|
2014-08-27 08:31:51 +08:00
|
|
|
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError => e
|
|
|
|
raise EmailUnparsableError.new(e)
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
|
2014-08-14 02:06:17 +08:00
|
|
|
def check_address(address)
|
|
|
|
category = Category.find_by_email(address)
|
|
|
|
return {type: :category, obj: category} if category
|
|
|
|
|
|
|
|
regex = Regexp.escape SiteSetting.reply_by_email_address
|
|
|
|
regex = regex.gsub(Regexp.escape('%{reply_key}'), "(.*)")
|
|
|
|
regex = Regexp.new regex
|
|
|
|
match = regex.match address
|
|
|
|
if match && match[1].present?
|
|
|
|
reply_key = match[1]
|
|
|
|
email_log = EmailLog.for(reply_key)
|
|
|
|
|
|
|
|
return {type: :reply, obj: email_log}
|
|
|
|
end
|
|
|
|
|
|
|
|
{type: :invalid, obj: nil}
|
|
|
|
end
|
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
def parse_body(message)
|
|
|
|
body = select_body message
|
2014-08-29 03:09:42 +08:00
|
|
|
encoding = body.encoding
|
2014-08-27 08:31:51 +08:00
|
|
|
raise EmptyEmailError if body.strip.blank?
|
2013-06-14 06:11:10 +08:00
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
body = discourse_email_trimmer body
|
|
|
|
raise EmptyEmailError if body.strip.blank?
|
2013-06-21 00:38:03 +08:00
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
body = EmailReplyParser.parse_reply body
|
|
|
|
raise EmptyEmailError if body.strip.blank?
|
|
|
|
|
2014-08-29 03:09:42 +08:00
|
|
|
body.force_encoding(encoding).encode("UTF-8")
|
2014-08-27 08:31:51 +08:00
|
|
|
end
|
2013-06-20 00:14:01 +08:00
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
def select_body(message)
|
|
|
|
html = nil
|
|
|
|
# If the message is multipart, return that part (favor html)
|
|
|
|
if message.multipart?
|
|
|
|
html = fix_charset message.html_part
|
|
|
|
text = fix_charset message.text_part
|
2014-11-26 00:44:59 +08:00
|
|
|
|
|
|
|
# prefer plain text
|
|
|
|
if text
|
2014-08-27 08:31:51 +08:00
|
|
|
return text
|
2014-01-17 10:24:32 +08:00
|
|
|
end
|
2014-08-27 08:31:51 +08:00
|
|
|
elsif message.content_type =~ /text\/html/
|
|
|
|
html = fix_charset message
|
2014-01-17 10:24:32 +08:00
|
|
|
end
|
2014-03-28 21:57:12 +08:00
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
if html
|
|
|
|
body = HtmlCleaner.new(html).output_html
|
|
|
|
else
|
|
|
|
body = fix_charset message
|
2013-06-21 00:38:03 +08:00
|
|
|
end
|
|
|
|
|
2015-05-23 03:40:26 +08:00
|
|
|
return body if @opts[:skip_sanity_check]
|
|
|
|
|
2013-11-21 02:29:42 +08:00
|
|
|
# Certain trigger phrases that means we didn't parse correctly
|
2014-08-27 08:31:51 +08:00
|
|
|
if body =~ /Content\-Type\:/ || body =~ /multipart\/alternative/ || body =~ /text\/plain/
|
|
|
|
raise EmptyEmailError
|
|
|
|
end
|
2013-11-21 02:29:42 +08:00
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
body
|
2013-06-21 00:38:03 +08:00
|
|
|
end
|
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
# Force encoding to UTF-8 on a Mail::Message or Mail::Part
|
|
|
|
def fix_charset(object)
|
|
|
|
return nil if object.nil?
|
2013-06-21 00:38:03 +08:00
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
if object.charset
|
2015-01-19 17:51:39 +08:00
|
|
|
object.body.decoded.force_encoding(object.charset.gsub(/utf8/i, "UTF-8")).encode("UTF-8").to_s
|
2014-08-27 08:31:51 +08:00
|
|
|
else
|
|
|
|
object.body.to_s
|
|
|
|
end
|
2015-07-24 06:37:40 +08:00
|
|
|
rescue
|
|
|
|
nil
|
2013-06-20 00:14:01 +08:00
|
|
|
end
|
2013-06-14 06:11:10 +08:00
|
|
|
|
2014-10-23 03:22:04 +08:00
|
|
|
REPLYING_HEADER_LABELS = ['From', 'Sent', 'To', 'Subject', 'Reply To', 'Cc', 'Bcc', 'Date']
|
2014-08-27 08:31:51 +08:00
|
|
|
REPLYING_HEADER_REGEX = Regexp.union(REPLYING_HEADER_LABELS.map { |lbl| "#{lbl}:" })
|
|
|
|
|
|
|
|
def discourse_email_trimmer(body)
|
|
|
|
lines = body.scrub.lines.to_a
|
2013-07-25 02:22:32 +08:00
|
|
|
range_end = 0
|
|
|
|
|
2013-12-30 11:05:25 +08:00
|
|
|
lines.each_with_index do |l, idx|
|
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") ||
|
2013-08-22 04:54:01 +08:00
|
|
|
(l =~ /via #{SiteSetting.title}(.*)\:$/) ||
|
2013-07-25 02:22:32 +08: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.
|
2014-09-06 09:26:45 +08:00
|
|
|
(l =~ /\d{4}/ && l =~ /\d:\d\d/ && l =~ /\:$/) ||
|
|
|
|
(l =~ /On \w+ \d+,? \d+,?.*wrote:/)
|
2013-07-25 02:22:32 +08:00
|
|
|
|
2014-08-27 08:31:51 +08: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
|
|
|
|
|
2013-07-25 02:22:32 +08:00
|
|
|
range_end = idx
|
|
|
|
end
|
|
|
|
|
2014-08-27 08:31:51 +08:00
|
|
|
lines[0..range_end].join.strip
|
2013-07-25 02:22:32 +08:00
|
|
|
end
|
|
|
|
|
2014-08-14 02:06:17 +08:00
|
|
|
def wrap_body_in_quote(user_email)
|
|
|
|
@body = "[quote=\"#{user_email}\"]
|
2014-04-15 04:55:57 +08:00
|
|
|
#{@body}
|
|
|
|
[/quote]"
|
|
|
|
end
|
|
|
|
|
2014-08-27 08:30:12 +08:00
|
|
|
private
|
|
|
|
|
2013-07-25 02:22:32 +08:00
|
|
|
def create_reply
|
2014-08-27 08:30:12 +08: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-11 04:46:08 +08:00
|
|
|
end
|
2014-02-25 00:36:53 +08:00
|
|
|
|
|
|
|
def create_new_topic
|
2015-04-29 01:04:34 +08:00
|
|
|
result = create_post_with_attachments(@user,
|
2014-08-27 08:30:12 +08:00
|
|
|
raw: @body,
|
|
|
|
title: @message.subject,
|
|
|
|
category: @category_id)
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2015-04-29 01:04:34 +08:00
|
|
|
topic_id = result.post.present? ? result.post.topic_id : nil
|
2014-04-15 04:55:57 +08:00
|
|
|
EmailLog.create(
|
|
|
|
email_type: "topic_via_incoming_email",
|
2014-08-27 08:30:12 +08:00
|
|
|
to_address: @message.from.first, # pick from address because we want the user's email
|
2015-04-29 01:04:34 +08:00
|
|
|
topic_id: topic_id,
|
2014-04-15 04:55:57 +08:00
|
|
|
user_id: @user.id,
|
|
|
|
)
|
|
|
|
|
2015-04-29 01:04:34 +08:00
|
|
|
result
|
2014-04-15 04:55:57 +08:00
|
|
|
end
|
|
|
|
|
2014-08-27 08:30:12 +08:00
|
|
|
def create_post_with_attachments(user, post_opts={})
|
2014-04-15 04:55:57 +08:00
|
|
|
options = {
|
|
|
|
cooking_options: { traditional_markdown_linebreaks: true },
|
2014-08-27 08:30:12 +08:00
|
|
|
}.merge(post_opts)
|
|
|
|
|
|
|
|
raw = options[:raw]
|
2014-04-15 04:55:57 +08: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-04 01:44:18 +08:00
|
|
|
upload = Upload.create_for(user.id, tmp, attachment.filename, tmp.size)
|
2014-04-15 04:55:57 +08:00
|
|
|
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
|
|
|
|
|
2014-08-27 08:30:12 +08:00
|
|
|
options[:raw] = raw
|
|
|
|
|
2014-04-15 04:55:57 +08:00
|
|
|
create_post(user, options)
|
|
|
|
end
|
|
|
|
|
2014-04-15 06:04:13 +08:00
|
|
|
def attachment_markdown(upload)
|
|
|
|
if FileHelper.is_image?(upload.original_filename)
|
2014-04-15 04:55:57 +08: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-05 01:04:22 +08:00
|
|
|
# Mark the reply as incoming via email
|
|
|
|
options[:via_email] = true
|
2014-10-16 14:08:02 +08:00
|
|
|
options[:raw_email] = @raw
|
2014-09-05 01:04:22 +08:00
|
|
|
|
2015-04-29 01:04:34 +08:00
|
|
|
manager = NewPostManager.new(user, options)
|
|
|
|
result = manager.perform
|
2014-08-27 08:30:12 +08:00
|
|
|
|
2015-04-29 01:04:34 +08:00
|
|
|
if result.errors.present?
|
|
|
|
raise InvalidPost, result.errors.full_messages.join("\n")
|
2014-07-31 16:46:02 +08:00
|
|
|
end
|
2014-08-27 08:30:12 +08:00
|
|
|
|
2015-04-29 01:04:34 +08:00
|
|
|
result
|
2014-02-24 14:01:37 +08:00
|
|
|
end
|
2013-06-11 04:46:08 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|