2008-09-20 19:15:12 -07:00
|
|
|
class MessageGateway < ActionMailer::Base
|
2008-09-20 19:20:08 -07:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
2008-06-04 18:16:06 -07:00
|
|
|
def receive(email)
|
|
|
|
|
user = User.find(:first, :include => [:preference], :conditions => ["preferences.sms_email = ?", email.from[0].strip])
|
2008-06-13 10:27:58 -07:00
|
|
|
if user.nil?
|
|
|
|
|
user = User.find(:first, :include => [:preference], :conditions => ["preferences.sms_email = ?", email.from[0].strip[1,100]])
|
|
|
|
|
end
|
2008-06-07 17:15:12 -07:00
|
|
|
return if user.nil?
|
2008-06-04 18:16:06 -07:00
|
|
|
context = user.prefs.sms_context
|
|
|
|
|
|
|
|
|
|
description = nil
|
|
|
|
|
notes = nil
|
|
|
|
|
|
|
|
|
|
if email.content_type == "multipart/related"
|
2008-09-20 19:20:08 -07:00
|
|
|
description = sanitize email.subject
|
2008-06-04 18:16:06 -07:00
|
|
|
body_part = email.parts.find{|m| m.content_type == "text/plain"}
|
2008-09-20 19:20:08 -07:00
|
|
|
notes = sanitize body_part.body.strip
|
2008-06-04 18:16:06 -07:00
|
|
|
else
|
|
|
|
|
if email.subject.empty?
|
2008-09-20 19:20:08 -07:00
|
|
|
description = sanitize email.body.strip
|
2008-06-04 18:16:06 -07:00
|
|
|
notes = nil
|
|
|
|
|
else
|
2008-09-20 19:20:08 -07:00
|
|
|
description = sanitize email.subject.strip
|
|
|
|
|
notes = sanitize email.body.strip
|
2008-06-04 18:16:06 -07:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# stupid T-Mobile often sends the same message multiple times
|
2008-06-14 22:12:13 -07:00
|
|
|
return if user.todos.find(:first, :conditions => {:description => description})
|
|
|
|
|
|
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
|
|
|
|
|
end
|