2008-09-20 19:15:12 -07:00
|
|
|
class MessageGateway < ActionMailer::Base
|
2012-04-24 20:47:07 +02:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
|
|
|
|
extend ActionView::Helpers::SanitizeHelper::ClassMethods
|
2008-11-29 12:00:06 -05:00
|
|
|
|
2008-06-04 18:16:06 -07:00
|
|
|
def receive(email)
|
2012-04-24 20:47:07 +02:00
|
|
|
user = get_user_from_email_address(email)
|
2008-06-07 17:15:12 -07:00
|
|
|
return if user.nil?
|
2012-04-24 20:47:07 +02:00
|
|
|
|
2008-06-04 18:16:06 -07:00
|
|
|
context = user.prefs.sms_context
|
|
|
|
|
description = nil
|
|
|
|
|
notes = nil
|
2012-04-24 20:47:07 +02:00
|
|
|
|
|
|
|
|
if email.multipart?
|
|
|
|
|
description = get_text_or_nil(email.subject)
|
|
|
|
|
notes = get_first_text_plain_part(email)
|
2008-06-04 18:16:06 -07:00
|
|
|
else
|
2012-04-24 20:47:07 +02:00
|
|
|
if email.subject.blank?
|
|
|
|
|
description = get_decoded_text_or_nil(email.body)
|
2008-06-04 18:16:06 -07:00
|
|
|
notes = nil
|
|
|
|
|
else
|
2012-04-24 20:47:07 +02:00
|
|
|
description = get_text_or_nil(email.subject)
|
|
|
|
|
notes = get_decoded_text_or_nil(email.body)
|
2008-06-04 18:16:06 -07:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# stupid T-Mobile often sends the same message multiple times
|
2012-04-17 16:47:37 +02:00
|
|
|
return if user.todos.where(:description => description).first
|
2008-06-14 22:12:13 -07:00
|
|
|
|
2008-09-21 09:36:32 -07:00
|
|
|
todo = Todo.from_rich_message(user, context.id, description, notes)
|
|
|
|
|
todo.save!
|
2008-06-04 18:16:06 -07:00
|
|
|
end
|
2012-04-24 20:47:07 +02:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def get_address(email)
|
|
|
|
|
return SITE_CONFIG['email_dispatch'] == 'to' ? email.to[0] : email.from[0]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_user_from_email_address(email)
|
|
|
|
|
address = get_address(email)
|
|
|
|
|
user = User.where("preferences.sms_email" => address.strip).includes(:preference).first
|
|
|
|
|
if user.nil?
|
|
|
|
|
user = User.where("preferences.sms_email" => address.strip[1.100]).includes(:preference).first
|
|
|
|
|
end
|
|
|
|
|
return user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_text_or_nil(text)
|
|
|
|
|
return text ? sanitize(text.strip) : nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_decoded_text_or_nil(text)
|
|
|
|
|
return text ? sanitize(text.decoded.strip) : nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_first_text_plain_part(email)
|
|
|
|
|
parts = email.parts.reject{|part| !part.content_type.start_with?("text/plain") }
|
|
|
|
|
return parts ? sanitize(parts[0].decoded.strip) : ""
|
|
|
|
|
end
|
|
|
|
|
|
2008-06-04 18:16:06 -07:00
|
|
|
end
|