diff --git a/app/mail_receivers/bounce_mail_receiver.rb b/app/mail_receivers/bounce_mail_receiver.rb index 73da529b..89cbced9 100644 --- a/app/mail_receivers/bounce_mail_receiver.rb +++ b/app/mail_receivers/bounce_mail_receiver.rb @@ -4,11 +4,14 @@ class BounceMailReceiver /bounce\+(?.*)=(?[^=]+)/ end - def received(match, data) - address = "#{match[:local]}@#{match[:domain]}" + def initialize(match) + @address = "#{match[:local]}@#{match[:domain]}" + end + + def received(data) mail = Mail.new data subject = mail.subject || 'Unknown bounce error' - MailDeliveryStatus.create email: address, + MailDeliveryStatus.create email: @address, message: subject, attachment_mime: 'message/rfc822', attachment_data: data diff --git a/lib/foodsoft_mail_receiver.rb b/lib/foodsoft_mail_receiver.rb index c404aad0..02f306a9 100644 --- a/lib/foodsoft_mail_receiver.rb +++ b/lib/foodsoft_mail_receiver.rb @@ -17,7 +17,7 @@ class FoodsoftMailReceiver < MidiSmtpServer::Smtpd @@registered_classes.each do |klass| klass_m = klass.regexp.match(m[:address]) - return klass.new.received klass_m, data if klass_m + return klass.new(klass_m).received(data) if klass_m end raise "invalid format for recipient" diff --git a/plugins/messages/app/mail_receivers/messages_mail_receiver.rb b/plugins/messages/app/mail_receivers/messages_mail_receiver.rb index ee358321..81d294e4 100644 --- a/plugins/messages/app/mail_receivers/messages_mail_receiver.rb +++ b/plugins/messages/app/mail_receivers/messages_mail_receiver.rb @@ -6,16 +6,18 @@ class MessagesMailReceiver /(?\d+)\.(?\d+)\.(?\w+)/ end - def received(match, data) - original_message = Message.find_by_id(match[:message_id]) - user = User.find_by_id(match[:user_id]) + def initialize(match) + @message = Message.find_by_id(match[:message_id]) + @user = User.find_by_id(match[:user_id]) - raise "Message could not be found" if original_message.nil? - raise "User could not be found" if user.nil? + raise "Message could not be found" if @message.nil? + raise "User could not be found" if @user.nil? - hash = original_message.mail_hash_for_user user + hash = @message.mail_hash_for_user(@user) raise "Hash does not match expectations" unless hash.casecmp(match[:hash]) == 0 + end + def received(data) mail = Mail.new data mail_part = nil @@ -41,18 +43,18 @@ class MessagesMailReceiver body = EmailReplyTrimmer.trim(body) - message = user.send_messages.new body: body, - group: original_message.group, - private: original_message.private, - received_email: received_email, + message = @user.send_messages.new body: body, + group: @message.group, + private: @message.private, + received_email: data, subject: mail.subject.gsub("[#{FoodsoftConfig[:name]}] ", "") - if original_message.reply_to - message.reply_to_message = original_message.reply_to_message + if @message.reply_to + message.reply_to_message = @message.reply_to_message else - message.reply_to_message = original_message + message.reply_to_message = @message end - message.add_recipients original_message.recipients - message.add_recipients [original_message.sender] + message.add_recipients @message.recipients + message.add_recipients [@message.sender] message.save! Resque.enqueue(MessageNotifier, FoodsoftConfig.scope, "message_deliver", message.id)