2023-05-12 13:01:12 +02:00
|
|
|
require 'email_reply_trimmer'
|
2017-09-30 01:20:12 +02:00
|
|
|
|
2017-08-19 15:45:48 +02:00
|
|
|
class MessagesMailReceiver
|
|
|
|
def self.regexp
|
|
|
|
/(?<message_id>\d+)\.(?<user_id>\d+)\.(?<hash>\w+)/
|
|
|
|
end
|
|
|
|
|
2017-10-02 16:16:02 +02:00
|
|
|
def initialize(match)
|
|
|
|
@message = Message.find_by_id(match[:message_id])
|
|
|
|
@user = User.find_by_id(match[:user_id])
|
2017-08-19 15:45:48 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
raise 'Message could not be found' if @message.nil?
|
|
|
|
raise 'User could not be found' if @user.nil?
|
2017-08-19 15:45:48 +02:00
|
|
|
|
2017-10-02 16:16:02 +02:00
|
|
|
hash = @message.mail_hash_for_user(@user)
|
2023-05-12 13:01:12 +02:00
|
|
|
raise 'Hash does not match expectations' unless hash.casecmp(match[:hash]) == 0
|
2017-10-02 16:16:02 +02:00
|
|
|
end
|
2017-08-19 15:45:48 +02:00
|
|
|
|
2017-10-02 16:16:02 +02:00
|
|
|
def received(data)
|
2017-08-19 15:45:48 +02:00
|
|
|
mail = Mail.new data
|
|
|
|
|
2017-10-19 14:57:42 +02:00
|
|
|
mail_part = get_mail_part(mail)
|
2023-05-12 13:01:12 +02:00
|
|
|
raise 'No valid content could be found' if mail_part.nil?
|
2017-08-19 15:45:48 +02:00
|
|
|
|
|
|
|
body = mail_part.body.decoded
|
2023-05-12 13:01:12 +02:00
|
|
|
body = body.force_encoding mail_part.content_type_parameters[:charset] unless mail_part.content_type_parameters.nil?
|
2017-08-19 15:45:48 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
body = Nokogiri::HTML(body).text if MIME::Type.simplified(mail_part.content_type) == 'text/html'
|
2017-09-30 01:18:02 +02:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
body.encode!(Encoding.default_internal)
|
2017-09-30 01:20:12 +02:00
|
|
|
body = EmailReplyTrimmer.trim(body)
|
2020-03-04 16:58:33 +01:00
|
|
|
raise BlankBodyException if body.empty?
|
2020-02-18 19:46:48 +01:00
|
|
|
|
2017-10-02 16:16:02 +02:00
|
|
|
message = @user.send_messages.new body: body,
|
2021-03-01 15:27:26 +01:00
|
|
|
group: @message.group,
|
|
|
|
private: @message.private,
|
|
|
|
received_email: data
|
2023-05-12 13:01:12 +02:00
|
|
|
message.reply_to_message = if @message.reply_to
|
|
|
|
@message.reply_to_message
|
|
|
|
else
|
|
|
|
@message
|
|
|
|
end
|
|
|
|
message.subject = if mail.subject
|
|
|
|
mail.subject.gsub("[#{FoodsoftConfig[:name]}] ", '')
|
|
|
|
else
|
|
|
|
I18n.t('messages.model.reply_subject', subject: message.reply_to_message.subject)
|
|
|
|
end
|
2019-10-29 01:19:05 +01:00
|
|
|
message.add_recipients [@message.sender_id]
|
2017-08-19 15:45:48 +02:00
|
|
|
|
|
|
|
message.save!
|
2020-07-31 14:09:45 +02:00
|
|
|
DeliverMessageJob.perform_later(message)
|
2017-08-19 15:45:48 +02:00
|
|
|
end
|
|
|
|
|
2017-10-19 14:57:42 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def get_mail_part(mail)
|
|
|
|
return mail unless mail.multipart?
|
|
|
|
|
|
|
|
mail_part = nil
|
|
|
|
for part in mail.parts
|
|
|
|
part = get_mail_part(part)
|
|
|
|
content_type = MIME::Type.simplified(part.content_type)
|
2023-05-12 13:01:12 +02:00
|
|
|
mail_part = part if content_type == 'text/plain' || (!mail_part && content_type == 'text/html')
|
2017-10-19 14:57:42 +02:00
|
|
|
end
|
|
|
|
mail_part
|
|
|
|
end
|
|
|
|
|
2020-03-04 16:58:33 +01:00
|
|
|
class BlankBodyException < MidiSmtpServer::SmtpdException
|
|
|
|
def initialize(msg = nil)
|
|
|
|
super msg, 541, 'The recipient address rejected your message because of a blank plain body'
|
|
|
|
end
|
|
|
|
end
|
2017-08-19 15:45:48 +02:00
|
|
|
end
|