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)
|
2012-07-12 13:14:21 +02:00
|
|
|
return false 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
|
|
|
|
|
|
2008-09-21 09:36:32 -07:00
|
|
|
todo = Todo.from_rich_message(user, context.id, description, notes)
|
|
|
|
|
todo.save!
|
2012-06-28 16:51:46 +02:00
|
|
|
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
|
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
|
|
|
|
|
|
2012-07-18 11:42:26 +02:00
|
|
|
def get_user_from_env_setting
|
|
|
|
|
Rails.logger.info "All received email goes to #{ENV['TRACKS_MAIL_RECEIVER']}"
|
|
|
|
|
user = User.find_by_login(ENV['TRACKS_MAIL_RECEIVER'])
|
|
|
|
|
Rails.logger.info "WARNING: Unknown user set for TRACKS_MAIL_RECEIVER (#{ENV['TRACKS_MAIL_RECEIVER']})" if user.nil?
|
|
|
|
|
return user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_user_from_mail_header(email)
|
|
|
|
|
address = get_address(email)
|
|
|
|
|
Rails.logger.info "Looking for user with email #{address}"
|
|
|
|
|
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
|
2012-04-24 20:47:07 +02:00
|
|
|
end
|
2012-07-18 11:42:26 +02:00
|
|
|
Rails.logger.info(!user.nil? ? "Email belongs to #{user.login}" : "User unknown")
|
2012-04-24 20:47:07 +02:00
|
|
|
return user
|
|
|
|
|
end
|
2012-07-18 11:42:26 +02:00
|
|
|
|
|
|
|
|
def get_user_from_email_address(email)
|
|
|
|
|
SITE_CONFIG['email_dispatch'] == 'single_user' ? get_user_from_env_setting : get_user_from_mail_header(email)
|
|
|
|
|
end
|
2012-04-24 20:47:07 +02:00
|
|
|
|
|
|
|
|
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
|