2011-08-26 22:59:43 +02:00
|
|
|
require 'iconv'
|
2011-09-09 22:10:25 +02:00
|
|
|
require 'mail'
|
2011-08-26 22:59:43 +02:00
|
|
|
|
2011-07-27 20:34:30 +02:00
|
|
|
class Message < ActiveRecord::Base
|
2011-08-26 22:59:43 +02:00
|
|
|
|
2011-08-02 23:12:17 +02:00
|
|
|
belongs_to :user
|
2011-08-16 20:05:58 +02:00
|
|
|
belongs_to :folder
|
2011-08-02 23:12:17 +02:00
|
|
|
|
2011-08-26 22:59:43 +02:00
|
|
|
set_primary_key :uid
|
2011-09-03 13:07:40 +02:00
|
|
|
attr_accessible :unseen, :to_addr, :size, :content_type, :folder_id, :subject, :date, :uid, :from_addr, :user_id, :msg_id, :body,:cc_addr,:bcc_addr
|
|
|
|
attr_accessor :body
|
2011-08-26 22:59:43 +02:00
|
|
|
|
|
|
|
def self.addr_to_db(addr)
|
|
|
|
ret = ""
|
|
|
|
name = addr.name
|
2011-08-28 21:52:17 +02:00
|
|
|
name.nil? ? ret : ret << ApplicationController.decode_quoted(name)
|
2011-08-26 22:59:43 +02:00
|
|
|
ret << "<" + addr.mailbox + "@" + addr.host
|
|
|
|
ret
|
|
|
|
end
|
2011-08-24 19:20:13 +02:00
|
|
|
|
2011-08-16 20:05:58 +02:00
|
|
|
def self.getPageForUser(user,folder,page,sort_field,sort_dir)
|
2011-08-02 23:12:17 +02:00
|
|
|
|
2011-08-16 20:05:58 +02:00
|
|
|
order = 'date desc'
|
|
|
|
if sort_field
|
|
|
|
if Message.attribute_method?(sort_field) == true
|
|
|
|
order = sort_field
|
|
|
|
sort_dir == 'desc' ? order += ' desc' : sort_dir
|
|
|
|
end
|
|
|
|
end
|
2011-08-02 23:12:17 +02:00
|
|
|
|
2011-08-16 20:05:58 +02:00
|
|
|
Message.paginate :page => page , :per_page => user.prefs.msgs_per_page.to_i, :conditions=> ['user_id = ? and folder_id = ?', user.id,folder.id],:order => order
|
|
|
|
end
|
|
|
|
|
2011-09-09 22:10:25 +02:00
|
|
|
def self.createForUser(user,folder,message)
|
2011-08-26 22:59:43 +02:00
|
|
|
|
2011-09-09 22:10:25 +02:00
|
|
|
# envelope = imap_message.attr['ENVELOPE']
|
|
|
|
#
|
|
|
|
# envelope.from.nil? ? from = "" : from = addr_to_db(envelope.from[0])
|
|
|
|
# envelope.to.nil? ? to = "" : to = addr_to_db(envelope.to[0])
|
|
|
|
# envelope.subject.nil? ? subject = "" : subject = ApplicationController.decode_quoted(envelope.subject)
|
2011-08-26 22:59:43 +02:00
|
|
|
|
2011-09-09 22:10:25 +02:00
|
|
|
mail = Mail.new(message.attr['RFC822.HEADER'])
|
2011-08-26 22:59:43 +02:00
|
|
|
|
2011-09-09 23:44:51 +02:00
|
|
|
mail.date.nil? ? date = nil : date = mail.date.to_s
|
|
|
|
mail.From.nil? ? from = nil : from = mail.From.charseted
|
|
|
|
mail.To.nil? ? to = nil : to = mail.To.charseted
|
|
|
|
mail.Subject.nil? ? subject = nil : subject = mail.Subject.charseted
|
|
|
|
|
2011-09-10 13:49:42 +02:00
|
|
|
#logger.custom('subject',mail.Subject.encoded)
|
|
|
|
#logger.custom('subject',subject)
|
2011-09-09 23:44:51 +02:00
|
|
|
#logger.custom('mail',mail.inspect)
|
|
|
|
|
2011-08-16 20:05:58 +02:00
|
|
|
create(
|
|
|
|
:user_id => user.id,
|
|
|
|
:folder_id => folder.id,
|
2011-09-09 22:10:25 +02:00
|
|
|
:msg_id => mail.message_id,
|
|
|
|
:uid => message.attr['UID'].to_i,
|
2011-09-09 23:44:51 +02:00
|
|
|
:from_addr => from,
|
|
|
|
:to_addr => to,
|
|
|
|
:subject => subject,
|
2011-09-09 22:10:25 +02:00
|
|
|
:content_type => mail.content_type,
|
2011-09-09 23:44:51 +02:00
|
|
|
:date => date,
|
2011-09-09 22:10:25 +02:00
|
|
|
:unseen => !(message.attr['FLAGS'].member? :Seen),
|
|
|
|
:size => message.attr['RFC822.SIZE']
|
2011-08-26 22:59:43 +02:00
|
|
|
)
|
|
|
|
end
|
2011-08-16 20:05:58 +02:00
|
|
|
|
|
|
|
def change_folder(folder)
|
|
|
|
update_attributes(:folder_id => folder.id)
|
2011-08-02 23:12:17 +02:00
|
|
|
end
|
|
|
|
|
2011-07-27 20:34:30 +02:00
|
|
|
end
|