2016-03-14 13:18:58 -04:00
require " digest "
2016-01-18 18:57:55 -05:00
require_dependency " new_post_manager "
require_dependency " post_action_creator "
2017-04-26 10:49:06 -04:00
require_dependency " html_to_markdown "
2017-12-05 19:47:31 -05:00
require_dependency " plain_text_to_markdown "
2017-05-10 18:16:57 -04:00
require_dependency " upload_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
2016-01-29 19:29:31 -05:00
include ActionView :: Helpers :: NumberHelper
2013-06-10 16:46:08 -04:00
2017-09-15 11:22:51 -04:00
# If you add a new error, you need to
# * add it to Email::Processor#handle_failure()
# * add text to server.en.yml (parent key: "emails.incoming.errors")
2016-04-18 16:58:30 -04:00
class ProcessingError < StandardError ; end
class EmptyEmailError < ProcessingError ; end
class ScreenedEmailError < ProcessingError ; end
class UserNotFoundError < ProcessingError ; end
class AutoGeneratedEmailError < ProcessingError ; end
class BouncedEmailError < ProcessingError ; end
class NoBodyDetectedError < ProcessingError ; end
2017-09-12 16:35:24 -04:00
class NoSenderDetectedError < ProcessingError ; end
2018-05-23 04:04:45 -04:00
class FromReplyByAddressError < ProcessingError ; end
2016-04-18 16:58:30 -04:00
class InactiveUserError < ProcessingError ; end
2017-11-10 12:18:08 -05:00
class SilencedUserError < ProcessingError ; end
2016-04-18 16:58:30 -04:00
class BadDestinationAddress < ProcessingError ; end
class StrangersNotAllowedError < ProcessingError ; end
class InsufficientTrustLevelError < ProcessingError ; end
class ReplyUserNotMatchingError < ProcessingError ; end
class TopicNotFoundError < ProcessingError ; end
class TopicClosedError < ProcessingError ; end
class InvalidPost < ProcessingError ; end
2018-08-02 15:43:53 -04:00
class TooShortPost < ProcessingError ; end
2016-04-18 16:58:30 -04:00
class InvalidPostAction < ProcessingError ; end
2017-10-03 04:13:19 -04:00
class UnsubscribeNotAllowed < ProcessingError ; end
2017-10-03 05:23:18 -04:00
class EmailNotAllowed < ProcessingError ; end
2018-05-09 12:51:01 -04:00
class OldDestinationError < ProcessingError ; end
2016-01-18 18:57:55 -05:00
2016-03-07 10:56:17 -05:00
attr_reader :incoming_email
2017-05-26 16:26:18 -04:00
attr_reader :raw_email
attr_reader :mail
attr_reader :message_id
2016-03-07 10:56:17 -05:00
2018-01-30 17:45:04 -05:00
COMMON_ENCODINGS || = [ - " utf-8 " , - " windows-1252 " , - " iso-8859-1 " ]
2017-11-15 10:39:29 -05:00
def self . formats
2018-05-09 12:51:01 -04:00
@formats || = Enum . new ( plaintext : 1 , markdown : 2 )
2017-11-15 10:39:29 -05:00
end
2017-12-05 19:47:31 -05:00
def initialize ( mail_string , opts = { } )
2016-01-18 18:57:55 -05:00
raise EmptyEmailError if mail_string . blank?
2017-10-03 04:13:19 -04:00
@staged_users = [ ]
2018-01-30 17:45:04 -05:00
@raw_email = mail_string
2018-10-10 21:46:32 -04:00
2018-01-30 17:45:04 -05:00
COMMON_ENCODINGS . each do | encoding |
fixed = try_to_encode ( mail_string , encoding )
break @raw_email = fixed if fixed . present?
end
2018-10-10 21:46:32 -04:00
2016-01-18 18:57:55 -05:00
@mail = Mail . new ( @raw_email )
2016-03-14 13:18:58 -04:00
@message_id = @mail . message_id . presence || Digest :: MD5 . hexdigest ( mail_string )
2017-12-05 19:47:31 -05:00
@opts = opts
2013-06-10 16:46:08 -04:00
end
2016-03-07 10:56:17 -05:00
def process!
2016-05-18 17:07:01 -04:00
return if is_blacklisted?
2017-05-17 19:09:51 -04:00
DistributedMutex . synchronize ( @message_id ) do
begin
2017-05-18 10:43:07 -04:00
return if IncomingEmail . exists? ( message_id : @message_id )
2018-03-27 12:28:37 -04:00
ensure_valid_address_lists
2018-11-30 01:59:51 -05:00
@from_email , @from_display_name = parse_from_field
2018-11-26 13:59:37 -05:00
@from_user = User . find_by_email ( @from_email )
2017-05-18 10:43:07 -04:00
@incoming_email = create_incoming_email
2017-05-17 19:09:51 -04:00
process_internal
2018-11-26 13:59:37 -05:00
raise BouncedEmailError if is_bounce?
2017-05-17 19:09:51 -04:00
rescue = > e
2017-08-04 10:20:44 -04:00
error = e . to_s
error = e . class . name if error . blank?
@incoming_email . update_columns ( error : error ) if @incoming_email
2017-10-03 11:28:41 -04:00
delete_staged_users
2017-05-17 19:09:51 -04:00
raise
end
end
2016-01-18 18:57:55 -05:00
end
2014-08-13 14:06:17 -04:00
2018-03-27 12:28:37 -04:00
def ensure_valid_address_lists
[ :to , :cc , :bcc ] . each do | field |
addresses = @mail [ field ]
if addresses & . errors . present?
@mail [ field ] = addresses . to_s . scan ( / \ b[A-Z0-9._%+-]+@[A-Z0-9.-]+ \ .[A-Z]{2,} \ b /i )
end
end
end
2016-05-18 17:07:01 -04:00
def is_blacklisted?
return false if SiteSetting . ignore_by_title . blank?
2017-11-11 19:43:18 -05:00
Regexp . new ( SiteSetting . ignore_by_title , Regexp :: IGNORECASE ) =~ @mail . subject
2016-05-18 17:07:01 -04:00
end
2017-05-18 10:43:07 -04:00
def create_incoming_email
IncomingEmail . create (
message_id : @message_id ,
raw : @raw_email ,
subject : subject ,
from_address : @from_email ,
to_addresses : @mail . to & . map ( & :downcase ) & . join ( " ; " ) ,
cc_addresses : @mail . cc & . map ( & :downcase ) & . join ( " ; " ) ,
)
2016-01-18 18:57:55 -05:00
end
2014-02-27 10:36:33 -05:00
2016-01-18 18:57:55 -05:00
def process_internal
2018-11-26 13:59:37 -05:00
handle_bounce if is_bounce?
2017-09-12 16:35:24 -04:00
raise NoSenderDetectedError if @from_email . blank?
2018-05-23 04:04:45 -04:00
raise FromReplyByAddressError if is_from_reply_by_email_address?
2016-04-18 16:58:30 -04:00
raise ScreenedEmailError if ScreenedEmail . should_block? ( @from_email )
2018-11-26 13:59:37 -05:00
user = @from_user
2016-03-23 13:56:03 -04:00
2017-10-03 04:13:19 -04:00
if user . present?
2017-10-03 05:23:18 -04:00
log_and_validate_user ( user )
2017-10-03 04:13:19 -04:00
else
raise UserNotFoundError unless SiteSetting . enable_staged_users
end
2016-04-20 15:29:27 -04:00
2016-11-16 13:42:11 -05:00
body , elided = select_body
2016-03-09 12:51:54 -05:00
body || = " "
2016-03-11 11:51:16 -05:00
2018-11-28 08:34:09 -05:00
raise NoBodyDetectedError if body . blank? && attachments . empty? && ! is_bounce?
2016-04-20 15:29:27 -04:00
2018-01-03 09:29:06 -05:00
if is_auto_generated? && ! sent_to_mailinglist_mirror?
2016-04-20 15:29:27 -04:00
@incoming_email . update_columns ( is_auto_generated : true )
2017-11-17 08:49:10 -05:00
2018-11-26 13:59:37 -05:00
if SiteSetting . block_auto_generated_emails? && ! is_bounce?
2017-11-17 08:49:10 -05:00
raise AutoGeneratedEmailError
end
2016-04-20 15:29:27 -04:00
end
2015-12-07 11:01:08 -05:00
2016-02-01 06:16:15 -05:00
if action = subscription_action_for ( body , subject )
2017-10-03 04:13:19 -04:00
raise UnsubscribeNotAllowed if user . nil?
send_subscription_mail ( action , user )
return
end
# Lets create a staged user if there isn't one yet. We will try to
# delete staged users in process!() if something bad happens.
2017-10-03 05:23:18 -04:00
if user . nil?
2018-10-08 03:45:23 -04:00
user = find_or_create_user! ( @from_email , @from_display_name )
2017-10-03 05:23:18 -04:00
log_and_validate_user ( user )
end
2017-10-03 04:13:19 -04:00
if post = find_related_post
2016-03-14 17:21:18 -04:00
create_reply ( user : user ,
raw : body ,
2016-11-16 13:42:11 -05:00
elided : elided ,
2018-07-05 05:07:46 -04:00
hidden_reason_id : hidden_reason_id ,
2016-03-14 17:21:18 -04:00
post : post ,
topic : post . topic ,
2018-11-26 13:59:37 -05:00
skip_validations : user . staged? ,
bounce : is_bounce? )
2016-01-18 18:57:55 -05:00
else
2016-08-03 09:57:37 -04:00
first_exception = nil
destinations . each do | destination |
begin
2018-11-26 13:59:37 -05:00
return process_destination ( destination , user , body , elided , hidden_reason_id )
2016-08-03 09:57:37 -04:00
rescue = > e
first_exception || = e
2016-06-16 22:01:08 -04:00
end
2016-01-18 18:57:55 -05:00
end
2016-08-03 09:57:37 -04:00
2018-05-09 12:51:01 -04:00
raise first_exception if first_exception
2018-08-21 04:17:08 -04:00
post = find_related_post ( force : true )
if post && Guardian . new ( user ) . can_see_post? ( post )
num_of_days = SiteSetting . disallow_reply_by_email_after_days
if num_of_days > 0 && post . created_at < num_of_days . days . ago
2018-05-09 12:51:01 -04:00
raise OldDestinationError . new ( " #{ Discourse . base_url } /p/ #{ post . id } " )
end
end
raise BadDestinationAddress
2016-01-18 18:57:55 -05:00
end
end
2014-08-26 20:31:51 -04:00
2018-11-28 10:43:06 -05:00
def hidden_reason_id
@hidden_reason_id || = is_spam? ? Post . hidden_reasons [ :email_spam_header_found ] : nil
end
2017-10-03 05:23:18 -04:00
def log_and_validate_user ( user )
2017-10-03 04:13:19 -04:00
@incoming_email . update_columns ( user_id : user . id )
raise InactiveUserError if ! user . active && ! user . staged
2017-11-10 12:18:08 -05:00
raise SilencedUserError if user . silenced?
2017-10-03 04:13:19 -04:00
end
2016-05-02 17:15:32 -04:00
def is_bounce?
2018-11-27 21:54:23 -05:00
@mail . bounced? || bounce_key
2018-11-26 13:59:37 -05:00
end
2016-05-02 17:15:32 -04:00
2018-11-26 13:59:37 -05:00
def handle_bounce
2016-05-02 17:15:32 -04:00
@incoming_email . update_columns ( is_bounce : true )
2018-11-28 08:34:09 -05:00
if email_log . present?
2016-07-15 12:00:40 -04:00
email_log . update_columns ( bounced : true )
2018-11-28 10:43:06 -05:00
post = email_log . post
2018-11-26 13:59:37 -05:00
topic = email_log . topic
2016-05-02 17:15:32 -04:00
end
2018-01-03 11:59:20 -05:00
if @mail . error_status . present? && Array . wrap ( @mail . error_status ) . any? { | s | s . start_with? ( " 4. " ) }
2018-11-28 08:34:09 -05:00
Email :: Receiver . update_bounce_score ( @from_email , SiteSetting . soft_bounce_score )
2016-07-15 12:00:40 -04:00
else
2018-11-28 08:34:09 -05:00
Email :: Receiver . update_bounce_score ( @from_email , SiteSetting . hard_bounce_score )
2016-06-28 10:42:05 -04:00
end
2018-11-28 10:43:06 -05:00
if SiteSetting . enable_whispers? && @from_user & . staged?
return if email_log . blank?
if post . present? && topic . present? && topic . archetype == Archetype . private_message
body , elided = select_body
body || = " "
create_reply ( user : @from_user ,
raw : body ,
elided : elided ,
hidden_reason_id : hidden_reason_id ,
post : post ,
topic : topic ,
skip_validations : true ,
bounce : true )
end
end
2018-11-26 13:59:37 -05:00
raise BouncedEmailError
2016-05-02 17:15:32 -04:00
end
2018-05-23 04:04:45 -04:00
def is_from_reply_by_email_address?
Email :: Receiver . reply_by_email_address_regex . match ( @from_email )
end
2018-11-27 21:54:23 -05:00
def bounce_key
@bounce_key || = begin
verp = all_destinations . select { | to | to [ / \ +verp- \ h{32}@ / ] } . first
verp && verp [ / \ +verp-( \ h{32})@ / , 1 ]
end
2016-05-02 17:15:32 -04:00
end
2018-11-28 08:34:09 -05:00
def email_log
2018-11-30 01:59:51 -05:00
return nil if bounce_key . blank?
2018-11-28 08:34:09 -05:00
@email_log || = EmailLog . find_by ( bounce_key : bounce_key )
end
2016-05-30 11:11:17 -04:00
def self . update_bounce_score ( email , score )
2018-05-09 10:40:52 -04:00
if user = User . find_by_email ( email )
old_bounce_score = user . user_stat . bounce_score
new_bounce_score = old_bounce_score + score
range = ( old_bounce_score + 1 .. new_bounce_score )
user . user_stat . bounce_score = new_bounce_score
user . user_stat . reset_bounce_score_after = SiteSetting . reset_bounce_score_after_days . days . from_now
user . user_stat . save!
if user . active && range === SiteSetting . bounce_score_threshold_deactivate
user . update! ( active : false )
reason = I18n . t ( " user.deactivated " , email : user . email )
StaffActionLogger . new ( Discourse . system_user ) . log_user_deactivate ( user , reason )
elsif range === SiteSetting . bounce_score_threshold
2018-08-03 10:39:22 -04:00
# NOTE: we check bounce_score before sending emails
# So log we revoked the email...
2018-05-09 10:40:52 -04:00
reason = I18n . t ( " user.email.revoked " , email : user . email , date : user . user_stat . reset_bounce_score_after )
StaffActionLogger . new ( Discourse . system_user ) . log_revoke_email ( user , reason )
2018-08-03 10:39:22 -04:00
# ... and PM the user
SystemMessage . create_from_system_user ( user , :email_revoked )
2016-05-02 17:15:32 -04:00
end
end
end
2016-01-18 18:57:55 -05:00
def is_auto_generated?
2016-04-11 16:47:34 -04:00
return false if SiteSetting . auto_generated_whitelist . split ( '|' ) . include? ( @from_email )
2016-03-30 12:41:09 -04:00
@mail [ :precedence ] . to_s [ / list|junk|bulk|auto_reply /i ] ||
2016-08-01 18:04:59 -04:00
@mail [ :from ] . to_s [ / (mailer[ \ -_]?daemon|post[ \ -_]?master|no[ \ -_]?reply)@ /i ] ||
@mail [ :subject ] . to_s [ / ^ \ s*(Auto:|Automatic reply|Autosvar|Automatisk svar|Automatisch antwoord|Abwesenheitsnotiz|Risposta Non al computer|Automatisch antwoord|Auto Response|Respuesta automática|Fuori sede|Out of Office|Frånvaro|Réponse automatique) /i ] ||
2016-03-30 12:41:09 -04:00
@mail . header . to_s [ / auto[ \ -_]?(response|submitted|replied|reply|generated|respond)|holidayreply|machinegenerated /i ]
2014-08-26 20:31:51 -04:00
end
2013-06-19 12:14:01 -04:00
2018-07-05 05:07:46 -04:00
def is_spam?
case SiteSetting . email_in_spam_header
when 'X-Spam-Flag'
@mail [ :x_spam_flag ] . to_s [ / YES /i ]
when 'X-Spam-Status'
@mail [ :x_spam_status ] . to_s [ / ^Yes, /i ]
else
false
end
end
2016-01-18 18:57:55 -05:00
def select_body
text = nil
2014-08-26 20:31:51 -04:00
html = nil
2017-12-05 19:47:31 -05:00
text_content_type = nil
2015-11-30 12:33:24 -05:00
2016-01-18 18:57:55 -05:00
if @mail . multipart?
text = fix_charset ( @mail . text_part )
html = fix_charset ( @mail . html_part )
2017-12-05 19:47:31 -05:00
text_content_type = @mail . text_part & . content_type
2016-01-18 18:57:55 -05:00
elsif @mail . content_type . to_s [ " text/html " ]
html = fix_charset ( @mail )
2018-02-16 12:14:56 -05:00
elsif @mail . content_type . blank? || @mail . content_type [ " text/plain " ]
2016-01-18 18:57:55 -05:00
text = fix_charset ( @mail )
2017-12-05 19:47:31 -05:00
text_content_type = @mail . content_type
2014-01-16 21:24:32 -05:00
end
2014-03-28 09:57:12 -04:00
2018-02-16 12:14:56 -05:00
return unless text . present? || html . present?
2017-12-05 19:47:31 -05:00
if text . present?
2016-06-06 04:30:04 -04:00
text = trim_discourse_markers ( text )
2018-01-17 06:03:57 -05:00
text , elided_text = trim_reply_and_extract_elided ( text )
2017-12-05 19:47:31 -05:00
if @opts [ :convert_plaintext ] || sent_to_mailinglist_mirror?
text_content_type || = " "
converter_opts = {
format_flowed : ! ! ( text_content_type =~ / format \ s*= \ s*["']?flowed["']? /i ) ,
delete_flowed_space : ! ! ( text_content_type =~ / DelSp \ s*= \ s*["']?yes["']? /i )
}
text = PlainTextToMarkdown . new ( text , converter_opts ) . to_markdown
elided_text = PlainTextToMarkdown . new ( elided_text , converter_opts ) . to_markdown
end
2016-06-06 04:30:04 -04:00
end
2017-04-26 10:49:06 -04:00
2017-04-27 08:31:11 -04:00
markdown , elided_markdown = if html . present?
2018-03-01 19:51:15 -05:00
# use the first html extracter that matches
if html_extracter = HTML_EXTRACTERS . select { | _ , r | html [ r ] } . min_by { | _ , r | html =~ r }
2018-04-13 13:04:27 -04:00
doc = Nokogiri :: HTML . fragment ( html )
self . send ( :" extract_from_ #{ html_extracter [ 0 ] } " , doc )
2018-02-26 17:54:02 -05:00
else
markdown = HtmlToMarkdown . new ( html , keep_img_tags : true , keep_cid_imgs : true ) . to_markdown
markdown = trim_discourse_markers ( markdown )
trim_reply_and_extract_elided ( markdown )
end
2017-04-27 08:31:11 -04:00
end
if text . blank? || ( SiteSetting . incoming_email_prefer_html && markdown . present? )
2017-11-15 10:39:29 -05:00
return [ markdown , elided_markdown , Receiver :: formats [ :markdown ] ]
2017-04-27 08:31:11 -04:00
else
2017-11-15 10:39:29 -05:00
return [ text , elided_text , Receiver :: formats [ :plaintext ] ]
2017-04-26 10:49:06 -04:00
end
2016-01-18 18:57:55 -05:00
end
2018-03-01 19:51:15 -05:00
def to_markdown ( html , elided_html )
markdown = HtmlToMarkdown . new ( html , keep_img_tags : true , keep_cid_imgs : true ) . to_markdown
[ EmailReplyTrimmer . trim ( markdown ) , HtmlToMarkdown . new ( elided_html ) . to_markdown ]
end
HTML_EXTRACTERS || = [
2018-07-04 14:04:46 -04:00
[ :gmail , / class="gmail_(signature|extra) / ] ,
2018-05-03 06:29:21 -04:00
[ :outlook , / id="(divRplyFwdMsg|Signature)" / ] ,
[ :word , / class="WordSection1" / ] ,
[ :exchange , / name="message(Body|Reply)Section" / ] ,
[ :apple_mail , / id="AppleMailSignature" / ] ,
[ :mozilla , / class="moz- / ] ,
[ :protonmail , / class="protonmail_ / ] ,
[ :zimbra , / data-marker="__ / ] ,
2018-04-19 06:39:55 -04:00
[ :newton , / (id|class)="cm_ / ] ,
2018-03-01 19:51:15 -05:00
]
2018-04-13 13:04:27 -04:00
def extract_from_gmail ( doc )
2018-07-04 14:04:46 -04:00
# GMail adds a bunch of 'gmail_' prefixed classes like: gmail_signature, gmail_extra, gmail_quote, gmail_default...
elided = doc . css ( " .gmail_signature, .gmail_extra " ) . remove
2018-03-01 19:51:15 -05:00
to_markdown ( doc . to_html , elided . to_html )
2018-02-27 09:00:50 -05:00
end
2018-04-13 13:04:27 -04:00
def extract_from_outlook ( doc )
2018-03-01 19:51:15 -05:00
# Outlook properly identifies the signature and any replied/forwarded email
# Use their id to remove them and anything that comes after
elided = doc . css ( " # Signature, # Signature ~ *, hr, # divRplyFwdMsg, # divRplyFwdMsg ~ * " ) . remove
to_markdown ( doc . to_html , elided . to_html )
end
2018-04-13 13:04:27 -04:00
def extract_from_word ( doc )
2018-03-01 19:51:15 -05:00
# Word (?) keeps the content in the 'WordSection1' class and uses <p> tags
# When there's something else (<table>, <div>, etc..) there's high chance it's a signature or forwarded email
elided = doc . css ( " .WordSection1 > :not(p):not(ul):first-of-type, .WordSection1 > :not(p):not(ul):first-of-type ~ * " ) . remove
to_markdown ( doc . at ( " .WordSection1 " ) . to_html , elided . to_html )
end
2018-04-13 13:04:27 -04:00
def extract_from_exchange ( doc )
2018-03-01 19:51:15 -05:00
# Exchange is using the 'messageReplySection' class for forwarded emails
# And 'messageBodySection' for the actual email
elided = doc . css ( " div[name='messageReplySection'] " ) . remove
2018-03-14 17:02:43 -04:00
to_markdown ( doc . css ( " div[name='messageReplySection'] " ) . to_html , elided . to_html )
2018-03-01 19:51:15 -05:00
end
2018-04-13 13:04:27 -04:00
def extract_from_apple_mail ( doc )
2018-03-01 19:51:15 -05:00
# AppleMail is the worst. It adds 'AppleMailSignature' ids (!) to several div/p with no deterministic rules
# Our best guess is to elide whatever comes after that.
2018-03-06 05:34:47 -05:00
elided = doc . css ( " # AppleMailSignature:last-of-type ~ * " ) . remove
2018-03-01 19:51:15 -05:00
to_markdown ( doc . to_html , elided . to_html )
end
2018-04-13 13:04:27 -04:00
def extract_from_mozilla ( doc )
2018-03-01 19:51:15 -05:00
# Mozilla (Thunderbird ?) properly identifies signature and forwarded emails
# Remove them and anything that comes after
elided = doc . css ( " *[class^='moz-'], *[class^='moz-'] ~ * " ) . remove
to_markdown ( doc . to_html , elided . to_html )
2018-02-26 17:54:02 -05:00
end
2018-04-13 13:04:27 -04:00
def extract_from_protonmail ( doc )
2018-03-30 04:41:32 -04:00
# Removes anything that has a class starting with "protonmail_" and everything after that
elided = doc . css ( " *[class^='protonmail_'], *[class^='protonmail_'] ~ * " ) . remove
to_markdown ( doc . to_html , elided . to_html )
end
2018-04-13 13:04:27 -04:00
def extract_from_zimbra ( doc )
# Removes anything that has a 'data-marker' attribute
elided = doc . css ( " *[data-marker] " ) . remove
to_markdown ( doc . to_html , elided . to_html )
end
2018-04-19 06:39:55 -04:00
def extract_from_newton ( doc )
# Removes anything that has an id or a class starting with 'cm_'
elided = doc . css ( " *[id^='cm_'], *[class^='cm_'] " ) . remove
to_markdown ( doc . to_html , elided . to_html )
end
2018-01-17 06:03:57 -05:00
def trim_reply_and_extract_elided ( text )
return [ text , " " ] if @opts [ :skip_trimming ]
EmailReplyTrimmer . trim ( text , true )
end
2016-01-18 18:57:55 -05:00
def fix_charset ( mail_part )
return nil if mail_part . blank? || mail_part . body . blank?
2016-01-29 19:29:31 -05:00
string = mail_part . body . decoded rescue nil
2013-06-20 12:38:03 -04:00
2016-01-29 19:29:31 -05:00
return nil if string . blank?
2015-05-22 15:40:26 -04:00
2016-06-26 07:27:34 -04:00
# common encodings
2018-01-30 17:45:04 -05:00
encodings = COMMON_ENCODINGS . dup
2016-06-26 07:27:34 -04:00
encodings . unshift ( mail_part . charset ) if mail_part . charset . present?
2017-04-30 17:30:40 -04:00
# mail (>=2.5) decodes mails with 8bit transfer encoding to utf-8, so
# always try UTF-8 first
if mail_part . content_transfer_encoding == " 8bit "
encodings . delete ( " UTF-8 " )
encodings . unshift ( " UTF-8 " )
end
2016-06-26 07:27:34 -04:00
encodings . uniq . each do | encoding |
fixed = try_to_encode ( string , encoding )
2016-01-18 18:57:55 -05:00
return fixed if fixed . present?
2014-08-26 20:31:51 -04:00
end
2013-11-20 13:29:42 -05:00
2016-06-26 07:27:34 -04:00
nil
2016-01-18 18:57:55 -05:00
end
def try_to_encode ( string , encoding )
2016-03-30 13:54:38 -04:00
encoded = string . encode ( " UTF-8 " , encoding )
2017-05-26 16:26:18 -04:00
! encoded . nil? && encoded . valid_encoding? ? encoded : nil
2016-03-11 12:51:53 -05:00
rescue Encoding :: InvalidByteSequenceError ,
Encoding :: UndefinedConversionError ,
Encoding :: ConverterNotFoundError
2016-01-18 18:57:55 -05:00
nil
2013-06-20 12:38:03 -04:00
end
2016-01-29 19:29:31 -05:00
def previous_replies_regex
2016-02-11 12:48:09 -05:00
@previous_replies_regex || = / ^--[- ] \ n \ * #{ I18n . t ( " user_notifications.previous_discussion " ) } \ * \ n /im
2016-01-29 19:29:31 -05:00
end
def trim_discourse_markers ( reply )
reply . split ( previous_replies_regex ) [ 0 ]
end
2018-11-30 01:59:51 -05:00
def parse_from_field ( mail = nil )
mail || = @mail
2018-11-28 08:34:09 -05:00
if mail . bounced?
2018-11-26 13:59:37 -05:00
Array . wrap ( mail . final_recipient ) . each do | from |
return extract_from_address_and_name ( from )
end
2018-11-28 08:34:09 -05:00
elsif email_log . present?
email = email_log . user & . email || email_log . to_address
return [ email , email_log . user & . username ]
2018-11-26 13:59:37 -05:00
end
2017-01-09 16:59:30 -05:00
return unless mail [ :from ]
2016-11-16 13:42:11 -05:00
if mail [ :from ] . errors . blank?
mail [ :from ] . address_list . addresses . each do | address_field |
address_field . decoded
from_address = address_field . address
from_display_name = address_field . display_name . try ( :to_s )
2016-12-01 12:34:47 -05:00
return [ from_address & . downcase , from_display_name & . strip ] if from_address [ " @ " ]
2016-11-16 13:42:11 -05:00
end
2016-02-24 11:40:57 -05:00
end
2016-11-16 13:42:11 -05:00
2017-09-12 16:35:24 -04:00
return extract_from_address_and_name ( mail . from ) if mail . from . is_a? String
if mail . from . is_a? Mail :: AddressContainer
mail . from . each do | from |
from_address , from_display_name = extract_from_address_and_name ( from )
return [ from_address , from_display_name ] if from_address
end
end
2017-09-15 10:47:19 -04:00
nil
rescue StandardError
2017-09-12 16:35:24 -04:00
nil
end
def extract_from_address_and_name ( value )
2018-11-26 13:59:37 -05:00
if value [ " ; " ]
from_display_name , from_address = value . split ( " ; " )
return [ from_address & . strip & . downcase , from_display_name & . strip ]
end
2017-09-12 16:35:24 -04:00
if value [ / <[^>]+> / ]
from_address = value [ / <([^>]+)> / , 1 ]
from_display_name = value [ / ^([^<]+) / , 1 ]
2016-12-01 12:34:47 -05:00
end
2017-09-12 16:35:24 -04:00
if ( from_address . blank? || ! from_address [ " @ " ] ) && value [ / \ [mailto:[^ \ ]]+ \ ] / ]
from_address = value [ / \ [mailto:([^ \ ]]+) \ ] / , 1 ]
from_display_name = value [ / ^([^ \ []+) / , 1 ]
2016-12-01 12:34:47 -05:00
end
2016-11-16 13:42:11 -05:00
2016-12-01 12:34:47 -05:00
[ from_address & . downcase , from_display_name & . strip ]
2016-01-18 18:57:55 -05:00
end
2016-02-01 06:16:15 -05:00
def subject
2018-10-10 22:45:01 -04:00
@subject || =
if mail_subject = @mail . subject
mail_subject . delete ( " \ u0000 " )
else
I18n . t ( " emails.incoming.default_subject " , email : @from_email )
end
2016-02-01 06:16:15 -05:00
end
2013-06-20 12:38:03 -04:00
2018-10-08 03:45:23 -04:00
def find_or_create_user ( email , display_name , raise_on_failed_create : false )
2016-03-23 13:56:03 -04:00
user = nil
User . transaction do
2017-10-03 05:23:18 -04:00
user = User . find_by_email ( email )
2016-04-18 16:58:30 -04:00
2017-10-03 05:23:18 -04:00
if user . nil? && SiteSetting . enable_staged_users
raise EmailNotAllowed unless EmailValidator . allowed? ( email )
2018-10-08 03:45:23 -04:00
username = UserNameSuggester . sanitize_username ( display_name ) if display_name . present?
2017-10-03 05:23:18 -04:00
begin
2016-04-18 16:58:30 -04:00
user = User . create! (
email : email ,
username : UserNameSuggester . suggest ( username . presence || email ) ,
name : display_name . presence || User . suggest_name ( email ) ,
staged : true
)
2017-10-03 04:13:19 -04:00
@staged_users << user
2018-10-08 03:45:23 -04:00
rescue PG :: UniqueViolation , ActiveRecord :: RecordNotUnique , ActiveRecord :: RecordInvalid
raise if raise_on_failed_create
2017-10-03 05:23:18 -04:00
user = nil
2016-04-18 16:58:30 -04:00
end
2016-03-23 13:56:03 -04:00
end
2014-08-26 20:31:51 -04:00
end
2016-03-23 13:56:03 -04:00
user
2013-06-19 12:14:01 -04:00
end
2013-06-13 18:11:10 -04:00
2018-10-08 03:45:23 -04:00
def find_or_create_user! ( email , display_name )
find_or_create_user ( email , display_name , raise_on_failed_create : true )
end
2016-05-06 13:34:33 -04:00
def all_destinations
@all_destinations || = [
@mail . destinations ,
2016-01-18 18:57:55 -05:00
[ @mail [ :x_forwarded_to ] ] . flatten . compact . map ( & :decoded ) ,
[ @mail [ :delivered_to ] ] . flatten . compact . map ( & :decoded ) ,
2016-05-06 13:34:33 -04:00
] . flatten . select ( & :present? ) . uniq . lazy
end
def destinations
2017-11-17 08:49:10 -05:00
@destinations || = all_destinations
2018-11-27 21:54:23 -05:00
. map { | d | Email :: Receiver . check_address ( d , is_bounce? ) }
2017-11-17 08:49:10 -05:00
. reject ( & :blank? )
end
def sent_to_mailinglist_mirror?
2018-08-21 03:44:47 -04:00
@sent_to_mailinglist_mirror || = begin
destinations . each do | destination |
next unless destination [ :type ] == :category
2017-11-17 08:49:10 -05:00
2018-08-21 03:44:47 -04:00
category = destination [ :obj ]
return true if category . mailinglist_mirror?
end
2017-11-17 08:49:10 -05:00
2018-08-21 03:44:47 -04:00
false
end
2015-11-18 15:22:50 -05:00
end
2018-11-27 21:54:23 -05:00
def self . check_address ( address , include_verp = false )
2016-01-18 18:57:55 -05:00
# only check for a group/category when 'email_in' is enabled
if SiteSetting . email_in
group = Group . find_by_email ( address )
return { type : :group , obj : group } if group
2013-07-24 14:22:32 -04:00
2016-01-18 18:57:55 -05:00
category = Category . find_by_email ( address )
return { type : :category , obj : category } if category
2015-11-18 15:22:50 -05:00
end
2016-01-18 18:57:55 -05:00
# reply
2018-11-27 21:54:23 -05:00
match = Email :: Receiver . reply_by_email_address_regex ( true , include_verp ) . match ( address )
2016-06-10 10:14:42 -04:00
if match && match . captures
match . captures . each do | c |
next if c . blank?
2018-07-18 04:28:44 -04:00
post_reply_key = PostReplyKey . find_by ( reply_key : c )
return { type : :reply , obj : post_reply_key } if post_reply_key
2016-06-10 10:14:42 -04:00
end
2013-07-24 14:22:32 -04:00
end
2017-06-08 14:28:48 -04:00
nil
2016-01-18 18:57:55 -05:00
end
2013-07-24 14:22:32 -04:00
2018-07-05 05:07:46 -04:00
def process_destination ( destination , user , body , elided , hidden_reason_id )
2016-11-16 13:42:11 -05:00
return if SiteSetting . enable_forwarded_emails &&
has_been_forwarded? &&
process_forwarded_email ( destination , user )
2018-11-26 13:59:37 -05:00
return if is_bounce? && destination [ :type ] != :reply
2016-08-03 09:57:37 -04:00
case destination [ :type ]
when :group
group = destination [ :obj ]
2018-07-05 05:07:46 -04:00
create_group_post ( group , user , body , elided , hidden_reason_id )
2016-08-03 09:57:37 -04:00
when :category
category = destination [ :obj ]
raise StrangersNotAllowedError if user . staged? && ! category . email_in_allow_strangers
2018-01-04 07:38:06 -05:00
raise InsufficientTrustLevelError if ! user . has_trust_level? ( SiteSetting . email_in_min_trust ) && ! sent_to_mailinglist_mirror?
2016-08-03 09:57:37 -04:00
create_topic ( user : user ,
raw : body ,
2017-06-29 00:03:14 -04:00
elided : elided ,
2018-07-05 05:07:46 -04:00
hidden_reason_id : hidden_reason_id ,
2016-08-03 09:57:37 -04:00
title : subject ,
category : category . id ,
skip_validations : user . staged? )
when :reply
2018-07-18 04:28:44 -04:00
post_reply_key = destination [ :obj ]
2016-08-03 09:57:37 -04:00
2018-07-18 04:28:44 -04:00
if post_reply_key . user_id != user . id && ! forwarded_reply_key? ( post_reply_key , user )
raise ReplyUserNotMatchingError , " post_reply_key.user_id => #{ post_reply_key . user_id . inspect } , user.id => #{ user . id . inspect } "
2016-08-03 09:57:37 -04:00
end
2018-09-03 17:06:25 -04:00
post = Post . with_deleted . find ( post_reply_key . post_id )
2016-08-03 09:57:37 -04:00
create_reply ( user : user ,
raw : body ,
2016-11-16 13:42:11 -05:00
elided : elided ,
2018-07-05 05:07:46 -04:00
hidden_reason_id : hidden_reason_id ,
2018-09-03 17:06:25 -04:00
post : post ,
topic : post & . topic ,
2018-11-26 13:59:37 -05:00
skip_validations : user . staged? ,
bounce : is_bounce? )
2016-08-03 09:57:37 -04:00
end
end
2018-07-05 05:07:46 -04:00
def create_group_post ( group , user , body , elided , hidden_reason_id )
2018-03-30 08:37:19 -04:00
message_ids = Email :: Receiver . extract_reply_message_ids ( @mail , max_message_id_count : 5 )
post_ids = [ ]
incoming_emails = IncomingEmail
. where ( message_id : message_ids )
. addressed_to_user ( user )
. pluck ( :post_id , :to_addresses , :cc_addresses )
incoming_emails . each do | post_id , to_addresses , cc_addresses |
post_ids << post_id if contains_email_address_of_user? ( to_addresses , user ) ||
contains_email_address_of_user? ( cc_addresses , user )
end
if post_ids . any? && post = Post . where ( id : post_ids ) . order ( :created_at ) . last
create_reply ( user : user ,
raw : body ,
elided : elided ,
2018-07-05 05:07:46 -04:00
hidden_reason_id : hidden_reason_id ,
2018-03-30 08:37:19 -04:00
post : post ,
topic : post . topic ,
skip_validations : true )
else
2018-10-15 19:51:57 -04:00
enable_email_pm_setting ( user )
2018-03-30 08:37:19 -04:00
create_topic ( user : user ,
raw : body ,
elided : elided ,
2018-07-05 05:07:46 -04:00
hidden_reason_id : hidden_reason_id ,
2018-03-30 08:37:19 -04:00
title : subject ,
archetype : Archetype . private_message ,
target_group_names : [ group . name ] ,
is_group_message : true ,
skip_validations : true )
end
end
2018-07-18 04:28:44 -04:00
def forwarded_reply_key? ( post_reply_key , user )
2017-11-12 17:44:22 -05:00
incoming_emails = IncomingEmail
. joins ( :post )
2018-07-18 04:28:44 -04:00
. where ( 'posts.topic_id = ?' , post_reply_key . post . topic_id )
. addressed_to ( post_reply_key . reply_key )
2018-03-30 08:37:19 -04:00
. addressed_to_user ( user )
. pluck ( :to_addresses , :cc_addresses )
2017-11-12 17:44:22 -05:00
2018-03-30 08:37:19 -04:00
incoming_emails . each do | to_addresses , cc_addresses |
next unless contains_email_address_of_user? ( to_addresses , user ) ||
contains_email_address_of_user? ( cc_addresses , user )
2017-11-12 17:44:22 -05:00
2018-07-18 04:28:44 -04:00
return true if contains_reply_by_email_address ( to_addresses , post_reply_key . reply_key ) ||
contains_reply_by_email_address ( cc_addresses , post_reply_key . reply_key )
2017-11-12 17:44:22 -05:00
end
false
end
2018-03-30 08:37:19 -04:00
def contains_email_address_of_user? ( addresses , user )
2017-11-12 17:44:22 -05:00
return false if addresses . blank?
2018-03-30 08:37:19 -04:00
addresses = addresses . split ( " ; " )
user . user_emails . any? { | user_email | addresses . include? ( user_email . email ) }
2017-11-12 17:44:22 -05:00
end
def contains_reply_by_email_address ( addresses , reply_key )
return false if addresses . blank?
addresses . split ( " ; " ) . each do | address |
match = Email :: Receiver . reply_by_email_address_regex . match ( address )
return true if match && match . captures & . include? ( reply_key )
end
false
end
2016-11-16 13:42:11 -05:00
def has_been_forwarded?
2017-02-08 15:38:52 -05:00
subject [ / ^[[:blank:]]*(fwd?|tr)[[:blank:]]?: /i ] && embedded_email_raw . present?
2016-11-16 13:42:11 -05:00
end
def embedded_email_raw
return @embedded_email_raw if @embedded_email_raw
text = fix_charset ( @mail . multipart? ? @mail . text_part : @mail )
@embedded_email_raw , @before_embedded = EmailReplyTrimmer . extract_embedded_email ( text )
@embedded_email_raw
end
def process_forwarded_email ( destination , user )
2017-01-06 09:32:25 -05:00
embedded = Mail . new ( embedded_email_raw )
2016-11-16 13:42:11 -05:00
email , display_name = parse_from_field ( embedded )
2016-12-01 12:34:47 -05:00
return false if email . blank? || ! email [ " @ " ]
2016-11-16 13:42:11 -05:00
embedded_user = find_or_create_user ( email , display_name )
2016-11-17 06:44:39 -05:00
raw = try_to_encode ( embedded . decoded , " UTF-8 " ) . presence || embedded . to_s
2016-11-16 13:42:11 -05:00
title = embedded . subject . presence || subject
case destination [ :type ]
when :group
group = destination [ :obj ]
post = create_topic ( user : embedded_user ,
raw : raw ,
title : title ,
archetype : Archetype . private_message ,
2016-12-01 12:34:47 -05:00
target_usernames : [ user . username ] ,
2016-11-16 13:42:11 -05:00
target_group_names : [ group . name ] ,
is_group_message : true ,
skip_validations : true ,
created_at : embedded . date )
when :category
category = destination [ :obj ]
return false if user . staged? && ! category . email_in_allow_strangers
return false if ! user . has_trust_level? ( SiteSetting . email_in_min_trust )
post = create_topic ( user : embedded_user ,
raw : raw ,
title : title ,
category : category . id ,
skip_validations : embedded_user . staged? ,
created_at : embedded . date )
else
return false
end
2017-01-06 09:32:25 -05:00
if post & . topic
# mark post as seen for the forwarder
PostTiming . record_timing ( user_id : user . id , topic_id : post . topic_id , post_number : post . post_number , msecs : 5000 )
2016-12-01 12:43:56 -05:00
2017-01-06 09:32:25 -05:00
# create reply when available
if @before_embedded . present?
post_type = Post . types [ :regular ]
post_type = Post . types [ :whisper ] if post . topic . private_message? && group . usernames [ user . username ]
create_reply ( user : user ,
raw : @before_embedded ,
post : post ,
topic : post . topic ,
2017-02-08 15:38:52 -05:00
post_type : post_type ,
skip_validations : user . staged? )
2017-01-06 09:32:25 -05:00
end
2016-11-16 13:42:11 -05:00
end
true
end
2018-11-27 21:54:23 -05:00
def self . reply_by_email_address_regex ( extract_reply_key = true , include_verp = false )
2017-05-22 17:35:41 -04:00
reply_addresses = [ SiteSetting . reply_by_email_address ]
reply_addresses << ( SiteSetting . alternative_reply_by_email_addresses . presence || " " ) . split ( " | " )
2018-11-27 21:54:23 -05:00
if include_verp && SiteSetting . reply_by_email_address . present? && SiteSetting . reply_by_email_address [ " + " ]
reply_addresses << SiteSetting . reply_by_email_address . sub ( " %{reply_key} " , " verp-%{reply_key} " )
end
2017-05-22 17:35:41 -04:00
reply_addresses . flatten!
reply_addresses . select! ( & :present? )
reply_addresses . map! { | a | Regexp . escape ( a ) }
2018-05-23 04:04:45 -04:00
reply_addresses . map! { | a | a . gsub ( " \ + " , " \ +? " ) }
reply_addresses . map! { | a | a . gsub ( Regexp . escape ( " %{reply_key} " ) , " ( \\ h{32})? " ) }
if reply_addresses . empty?
/ $a / # a regex that can never match
else
/ #{ reply_addresses . join ( " | " ) } /
end
2013-07-24 14:22:32 -04:00
end
2016-01-20 17:08:27 -05:00
def group_incoming_emails_regex
2016-02-24 13:47:58 -05:00
@group_incoming_emails_regex || = Regexp . union Group . pluck ( :incoming_email ) . select ( & :present? ) . map { | e | e . split ( " | " ) } . flatten . uniq
2016-01-20 17:08:27 -05:00
end
def category_email_in_regex
2016-02-24 13:47:58 -05:00
@category_email_in_regex || = Regexp . union Category . pluck ( :email_in ) . select ( & :present? ) . map { | e | e . split ( " | " ) } . flatten . uniq
2016-01-20 17:08:27 -05:00
end
2018-05-09 12:51:01 -04:00
def find_related_post ( force : false )
return if ! force && SiteSetting . find_related_post_with_key && ! sent_to_mailinglist_mirror?
2017-06-19 07:12:55 -04:00
2018-03-30 08:37:19 -04:00
message_ids = Email :: Receiver . extract_reply_message_ids ( @mail , max_message_id_count : 5 )
2016-01-18 18:57:55 -05:00
return if message_ids . empty?
2017-02-08 15:38:52 -05:00
host = Email :: Sender . host_for ( Discourse . base_url )
post_id_regexp = Regexp . new " topic/ \\ d+/( \\ d+)@ #{ Regexp . escape ( host ) } "
topic_id_regexp = Regexp . new " topic/( \\ d+)@ #{ Regexp . escape ( host ) } "
2017-02-08 17:46:11 -05:00
post_ids = message_ids . map { | message_id | message_id [ post_id_regexp , 1 ] } . compact . map ( & :to_i )
2017-02-08 15:38:52 -05:00
post_ids << Post . where ( topic_id : message_ids . map { | message_id | message_id [ topic_id_regexp , 1 ] } . compact , post_number : 1 ) . pluck ( :id )
post_ids << EmailLog . where ( message_id : message_ids ) . pluck ( :post_id )
post_ids << IncomingEmail . where ( message_id : message_ids ) . pluck ( :post_id )
post_ids . flatten!
post_ids . compact!
post_ids . uniq!
return if post_ids . empty?
Post . where ( id : post_ids ) . order ( :created_at ) . last
2016-01-18 18:57:55 -05:00
end
2015-11-24 10:58:26 -05:00
2018-03-30 08:37:19 -04:00
def self . extract_reply_message_ids ( mail , max_message_id_count : )
message_ids = [ mail . in_reply_to , Email :: Receiver . extract_references ( mail . references ) ]
message_ids . flatten!
message_ids . select! ( & :present? )
message_ids . uniq!
message_ids . first ( max_message_id_count )
end
2016-02-10 16:00:27 -05:00
def self . extract_references ( references )
if Array === references
references
elsif references . present?
2017-02-08 15:38:52 -05:00
references . split ( / [ \ s,] / ) . map { | r | r . tr ( " <> " , " " ) }
2016-01-18 18:57:55 -05:00
end
2014-04-14 16:55:57 -04:00
end
2016-01-18 18:57:55 -05:00
def likes
2016-07-05 09:59:23 -04:00
@likes || = Set . new [ " +1 " , " <3 " , " ❤ " , I18n . t ( 'post_action_types.like.title' ) . downcase ]
2015-12-30 06:17:45 -05:00
end
2016-01-20 04:25:25 -05:00
def subscription_action_for ( body , subject )
return unless SiteSetting . unsubscribe_via_email
2018-10-11 10:08:57 -04:00
return if sent_to_mailinglist_mirror?
2016-01-20 04:25:25 -05:00
if ( [ subject , body ] . compact . map ( & :to_s ) . map ( & :downcase ) & [ 'unsubscribe' ] ) . any?
:confirm_unsubscribe
end
end
2015-12-30 06:17:45 -05:00
def post_action_for ( body )
2017-05-22 17:35:41 -04:00
PostActionType . types [ :like ] if likes . include? ( body . strip . downcase )
2015-12-30 06:17:45 -05:00
end
2017-07-27 21:20:09 -04:00
def create_topic ( options = { } )
2016-01-18 18:57:55 -05:00
create_post_with_attachments ( options )
2013-06-10 16:46:08 -04:00
end
2014-02-24 11:36:53 -05:00
2017-07-27 21:20:09 -04:00
def create_reply ( options = { } )
2016-01-18 18:57:55 -05:00
raise TopicNotFoundError if options [ :topic ] . nil? || options [ :topic ] . trashed?
2018-11-26 13:59:37 -05:00
raise BouncedEmailError if options [ :bounce ] && options [ :topic ] . archetype != Archetype . private_message
2018-09-03 17:06:25 -04:00
options [ :post ] = nil if options [ :post ] & . trashed?
2018-10-15 19:51:57 -04:00
enable_email_pm_setting ( options [ :user ] ) if options [ :topic ] . archetype == Archetype . private_message
2014-04-14 16:55:57 -04:00
2016-01-18 18:57:55 -05:00
if post_action_type = post_action_for ( options [ :raw ] )
create_post_action ( options [ :user ] , options [ :post ] , post_action_type )
else
2016-07-05 11:33:08 -04:00
raise TopicClosedError if options [ :topic ] . closed?
2018-09-03 17:06:25 -04:00
options [ :topic_id ] = options [ :topic ] . id
options [ :reply_to_post_number ] = options [ :post ] & . post_number
2016-02-29 16:39:24 -05:00
options [ :is_group_message ] = options [ :topic ] . private_message? && options [ :topic ] . allowed_groups . exists?
2016-01-18 18:57:55 -05:00
create_post_with_attachments ( options )
end
2014-04-14 16:55:57 -04:00
end
2016-01-18 18:57:55 -05:00
def create_post_action ( user , post , type )
PostActionCreator . new ( user , post ) . perform ( type )
rescue PostAction :: AlreadyActed
# it's cool, don't care
rescue Discourse :: InvalidAccess = > e
raise InvalidPostAction . new ( e )
end
2014-04-14 16:55:57 -04:00
2018-02-16 12:14:56 -05:00
def is_whitelisted_attachment? ( attachment )
attachment . content_type !~ SiteSetting . attachment_content_type_blacklist_regex &&
attachment . filename !~ SiteSetting . attachment_filename_blacklist_regex
end
2016-08-08 06:30:37 -04:00
def attachments
# strip blacklisted attachments (mostly signatures)
2018-02-16 12:14:56 -05:00
@attachments || = begin
2018-11-01 03:41:13 -04:00
attachments = @mail . parts . select { | part | part . attachment? && is_whitelisted_attachment? ( part ) }
2018-02-16 12:14:56 -05:00
attachments << @mail if @mail . attachment? && is_whitelisted_attachment? ( @mail )
attachments
2016-08-08 06:30:37 -04:00
end
end
2016-08-03 11:55:54 -04:00
2017-07-27 21:20:09 -04:00
def create_post_with_attachments ( options = { } )
2014-04-14 16:55:57 -04:00
# deal with attachments
2018-10-17 10:48:09 -04:00
options [ :raw ] = add_attachments ( options [ :raw ] , options [ :user ] , options )
2017-10-06 08:28:26 -04:00
create_post ( options )
end
2018-10-17 10:48:09 -04:00
def add_attachments ( raw , user , options = { } )
2018-10-04 10:08:28 -04:00
rejected_attachments = [ ]
2016-08-08 06:30:37 -04:00
attachments . each do | attachment |
2017-04-24 00:06:28 -04:00
tmp = Tempfile . new ( [ " discourse-email-attachment " , File . extname ( attachment . filename ) ] )
2014-04-14 16:55:57 -04:00
begin
# read attachment
File . open ( tmp . path , " w+b " ) { | f | f . write attachment . body . decoded }
# create the upload for the user
2017-06-12 16:41:29 -04:00
opts = { for_group_message : options [ :is_group_message ] }
2018-10-17 10:48:09 -04:00
upload = UploadCreator . new ( tmp , attachment . filename , opts ) . create_for ( user . id )
2017-11-07 13:17:33 -05:00
if upload & . valid?
2015-11-30 12:33:24 -05:00
# try to inline images
2017-10-06 08:28:26 -04:00
if attachment . content_type & . start_with? ( " image/ " )
if raw [ attachment . url ]
raw . sub! ( attachment . url , upload . url )
elsif raw [ / \ [image:.*? \ d+[^ \ ]]* \ ] /i ]
raw . sub! ( / \ [image:.*? \ d+[^ \ ]]* \ ] /i , attachment_markdown ( upload ) )
2017-05-03 16:54:26 -04:00
else
2017-10-06 08:28:26 -04:00
raw << " \n \n #{ attachment_markdown ( upload ) } \n \n "
2017-05-03 16:54:26 -04:00
end
2016-01-18 18:57:55 -05:00
else
2017-10-06 08:28:26 -04:00
raw << " \n \n #{ attachment_markdown ( upload ) } \n \n "
2015-11-30 12:33:24 -05:00
end
2018-10-04 10:08:28 -04:00
else
rejected_attachments << upload
raw << " \n \n #{ I18n . t ( 'emails.incoming.missing_attachment' , filename : upload . original_filename ) } \n \n "
2014-04-14 16:55:57 -04:00
end
ensure
2018-03-28 04:20:08 -04:00
tmp & . close!
2014-04-14 16:55:57 -04:00
end
end
2018-10-04 10:08:28 -04:00
notify_about_rejected_attachment ( rejected_attachments ) if rejected_attachments . present? && ! user . staged?
2014-04-14 16:55:57 -04:00
2017-10-06 08:28:26 -04:00
raw
2014-04-14 16:55:57 -04:00
end
2018-10-04 10:08:28 -04:00
def notify_about_rejected_attachment ( attachments )
errors = [ ]
attachments . each do | a |
error = a . errors . messages . values [ 0 ] [ 0 ]
errors << " #{ a . original_filename } : #{ error } "
end
message = Mail :: Message . new ( @mail )
template_args = {
former_title : message . subject ,
destination : message . to ,
site_name : SiteSetting . title ,
rejected_errors : errors . join ( " \n " )
}
client_message = RejectionMailer . send_rejection ( :email_reject_attachment , message . from , template_args )
Email :: Sender . new ( client_message , :email_reject_attachment ) . send
end
2014-04-14 18:04:13 -04:00
def attachment_markdown ( upload )
2018-09-09 22:22:45 -04:00
if FileHelper . is_supported_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
2017-07-27 21:20:09 -04:00
def create_post ( options = { } )
2014-09-04 13:04:22 -04:00
options [ :via_email ] = true
2016-01-18 18:57:55 -05:00
options [ :raw_email ] = @raw_email
2014-09-04 13:04:22 -04:00
2016-01-18 18:57:55 -05:00
# ensure posts aren't created in the future
2016-11-16 13:42:11 -05:00
options [ :created_at ] || = @mail . date
2017-01-12 19:05:00 -05:00
if options [ :created_at ] . nil?
raise InvalidPost , " No post creation date found. Is the e-mail missing a Date: header? "
end
2017-07-27 21:20:09 -04:00
options [ :created_at ] = DateTime . now if options [ :created_at ] > DateTime . now
2016-01-18 18:57:55 -05:00
2016-06-06 04:30:04 -04:00
is_private_message = options [ :archetype ] == Archetype . private_message ||
options [ :topic ] . try ( :private_message? )
2016-03-17 18:10:46 -04:00
# only add elided part in messages
2016-11-16 16:06:07 -05:00
if options [ :elided ] . present? && ( SiteSetting . always_show_trimmed_content || is_private_message )
2017-05-26 16:26:18 -04:00
options [ :raw ] << Email :: Receiver . elided_html ( options [ :elided ] )
2016-03-17 18:10:46 -04:00
end
2018-01-04 07:38:06 -05:00
if sent_to_mailinglist_mirror?
options [ :skip_validations ] = true
options [ :skip_guardian ] = true
end
2016-04-11 12:20:26 -04:00
user = options . delete ( :user )
2018-11-26 13:59:37 -05:00
if options [ :bounce ]
options [ :raw ] = I18n . t ( " system_messages.email_bounced " , email : user . email , raw : options [ :raw ] )
user = Discourse . system_user
options [ :post_type ] = Post . types [ :whisper ]
end
2017-01-06 09:32:25 -05:00
result = NewPostManager . new ( user , options ) . perform
2014-08-26 20:30:12 -04:00
2018-08-02 15:43:53 -04:00
errors = result . errors . full_messages
if errors . any? do | message |
message . include? ( I18n . t ( " activerecord.attributes.post.raw " ) . strip ) &&
message . include? ( I18n . t ( " errors.messages.too_short " , count : SiteSetting . min_post_length ) . strip )
end
raise TooShortPost
end
2018-11-09 12:24:28 -05:00
2018-08-02 15:43:53 -04:00
raise InvalidPost , errors . join ( " \n " ) if result . errors . any?
2016-01-18 18:57:55 -05:00
if result . post
@incoming_email . update_columns ( topic_id : result . post . topic_id , post_id : result . post . id )
2018-11-27 21:54:23 -05:00
if result . post . topic & . private_message? && ! is_bounce?
2017-10-06 10:37:28 -04:00
add_other_addresses ( result . post , user )
2016-01-18 18:57:55 -05:00
end
2014-07-31 04:46:02 -04:00
end
2016-11-16 13:42:11 -05:00
result . post
2016-01-18 18:57:55 -05:00
end
2014-08-26 20:30:12 -04:00
2017-05-26 16:26:18 -04:00
def self . elided_html ( elided )
html = " \n \n " << " <details class='elided'> " << " \n "
2017-12-05 19:47:31 -05:00
html << " <summary title=' #{ I18n . t ( 'emails.incoming.show_trimmed_content' ) } '>& # 183;& # 183;& # 183;</summary> " << " \n \n "
html << elided << " \n \n "
2017-05-26 16:26:18 -04:00
html << " </details> " << " \n "
html
end
2017-10-06 10:37:28 -04:00
def add_other_addresses ( post , sender )
2016-01-18 18:57:55 -05:00
% i ( to cc bcc ) . each do | d |
if @mail [ d ] && @mail [ d ] . address_list && @mail [ d ] . address_list . addresses
2016-01-19 09:24:34 -05:00
@mail [ d ] . address_list . addresses . each do | address_field |
2016-01-18 18:57:55 -05:00
begin
2016-02-24 11:40:57 -05:00
address_field . decoded
2016-01-19 09:24:34 -05:00
email = address_field . address . downcase
2016-02-24 11:40:57 -05:00
display_name = address_field . display_name . try ( :to_s )
2016-11-16 13:42:11 -05:00
next unless email [ " @ " ]
2016-01-20 17:08:27 -05:00
if should_invite? ( email )
2016-02-24 11:40:57 -05:00
user = find_or_create_user ( email , display_name )
2017-10-06 10:37:28 -04:00
if user && can_invite? ( post . topic , user )
post . topic . topic_allowed_users . create! ( user_id : user . id )
TopicUser . auto_notification_for_staging ( user . id , post . topic_id , TopicUser . notification_reasons [ :auto_watch ] )
post . topic . add_small_action ( sender , " invited_user " , user . username )
2016-01-18 18:57:55 -05:00
end
2016-05-16 15:45:34 -04:00
# cap number of staged users created per email
2017-10-03 04:13:19 -04:00
if @staged_users . count > SiteSetting . maximum_staged_users_per_email
2017-10-06 10:37:28 -04:00
post . topic . add_moderator_post ( sender , I18n . t ( " emails.incoming.maximum_staged_user_per_email_reached " ) )
2016-05-16 15:45:34 -04:00
return
end
2016-01-18 18:57:55 -05:00
end
2017-10-03 05:23:18 -04:00
rescue ActiveRecord :: RecordInvalid , EmailNotAllowed
# don't care if user already allowed or the user's email address is not allowed
2016-01-18 18:57:55 -05:00
end
end
end
end
2014-02-24 01:01:37 -05:00
end
2013-06-10 16:46:08 -04:00
2016-01-20 17:08:27 -05:00
def should_invite? ( email )
2017-04-05 12:45:58 -04:00
email !~ Email :: Receiver . reply_by_email_address_regex &&
2016-01-20 17:08:27 -05:00
email !~ group_incoming_emails_regex &&
email !~ category_email_in_regex
end
2016-01-19 09:24:34 -05:00
def can_invite? ( topic , user )
! topic . topic_allowed_users . where ( user_id : user . id ) . exists? &&
! topic . topic_allowed_groups . where ( " group_id IN (SELECT group_id FROM group_users WHERE user_id = ?) " , user . id ) . exists?
end
2017-10-03 04:13:19 -04:00
def send_subscription_mail ( action , user )
message = SubscriptionMailer . send ( action , user )
Email :: Sender . new ( message , :subscription ) . send
end
2017-10-03 11:28:41 -04:00
def delete_staged_users
@staged_users . each do | user |
2017-10-31 10:13:23 -04:00
if @incoming_email . user . id == user . id
@incoming_email . update_columns ( user_id : nil )
end
if user . posts . count == 0
UserDestroyer . new ( Discourse . system_user ) . destroy ( user , quiet : true )
end
2017-10-03 11:28:41 -04:00
end
end
2018-10-15 19:51:57 -04:00
def enable_email_pm_setting ( user )
# ensure user PM emails are enabled (since user is posting via email)
if ! user . staged && ! user . user_option . email_private_messages
user . user_option . update! ( email_private_messages : true )
end
end
2013-06-10 16:46:08 -04:00
end
2016-01-18 18:57:55 -05:00
2013-06-10 16:46:08 -04:00
end