mailr/app/controllers/messages_controller.rb

211 lines
7.3 KiB
Ruby
Raw Normal View History

2011-07-27 20:34:30 +02:00
require 'imap_session'
require 'imap_mailbox'
2011-08-02 23:12:17 +02:00
require 'imap_message'
2011-08-24 19:20:13 +02:00
require 'mail'
2011-09-03 13:07:40 +02:00
require 'mail_plugin_extension'
2011-07-24 22:22:13 +02:00
2011-07-22 22:57:36 +02:00
class MessagesController < ApplicationController
2011-07-23 21:55:26 +02:00
2011-07-27 20:34:30 +02:00
include ImapMailboxModule
include ImapSessionModule
2011-08-02 23:12:17 +02:00
include ImapMessageModule
include MessagesHelper
2011-07-27 20:34:30 +02:00
2011-08-24 19:20:13 +02:00
before_filter :check_current_user ,:selected_folder,:get_current_folders
2011-08-16 20:05:58 +02:00
before_filter :open_imap_session, :select_imap_folder
2011-09-05 19:08:22 +02:00
before_filter :prepare_compose_buttons, :only => [:compose]
before_filter :get_system_folders, :only => [:index]
2011-09-09 22:10:25 +02:00
before_filter :create_message_with_params, :only => [:compose]
before_filter :prepare_multi1_buttons, :only => [:index,:show]
before_filter :prepare_multi2_buttons, :only => [:index]
before_filter :prepare_multi3_buttons, :only => [:show]
2011-08-16 20:05:58 +02:00
after_filter :close_imap_session
2011-07-24 22:22:13 +02:00
2011-07-23 21:55:26 +02:00
theme :theme_resolver
def index
2011-08-02 23:12:17 +02:00
2011-09-03 13:07:40 +02:00
if @sent_folder.nil? || @drafts_folder.nil? || @inbox_folder.nil? || @trash_folder.nil?
flash[:warning] = t(:not_all_configured,:scope => :folder)
end
2011-08-24 19:20:13 +02:00
if @current_folder.nil?
2011-09-03 13:07:40 +02:00
flash[:warning] = t(:no_selected,:scope => :folder)
2011-08-24 19:20:13 +02:00
redirect_to :controller => 'folders', :action => 'index'
return
end
2011-08-02 23:12:17 +02:00
@messages = []
2011-08-16 20:05:58 +02:00
folder_status = @mailbox.status
@current_folder.update_attributes(:total => folder_status['MESSAGES'], :unseen => folder_status['UNSEEN'])
2011-08-02 23:12:17 +02:00
2011-08-16 20:05:58 +02:00
folder_status['MESSAGES'].zero? ? uids_remote = [] : uids_remote = @mailbox.fetch_uids
uids_local = @current_user.messages.where(:folder_id => @current_folder).collect(&:uid)
2011-08-02 23:12:17 +02:00
2011-09-03 13:07:40 +02:00
logger.custom('current_folder',@current_folder.inspect)
logger.custom('uids_local',uids_local.join(","))
logger.custom('uids_remote',uids_remote.join(","))
logger.custom('to_delete',(uids_local-uids_remote).join(","))
logger.custom('to_fetch',(uids_remote-uids_local).join(","))
2011-08-16 20:05:58 +02:00
(uids_local-uids_remote).each do |uid|
@current_folder.messages.find_by_uid(uid).destroy
2011-08-02 23:12:17 +02:00
end
2011-08-16 20:05:58 +02:00
(uids_remote-uids_local).each_slice($defaults["imap_fetch_slice"].to_i) do |slice|
messages = @mailbox.uid_fetch(slice, ImapMessageModule::IMAPMessage.fetch_attr)
2011-08-02 23:12:17 +02:00
messages.each do |m|
2011-08-26 22:59:43 +02:00
Message.createForUser(@current_user,@current_folder,m)
2011-08-02 23:12:17 +02:00
end
end
2011-08-16 20:05:58 +02:00
@messages = Message.getPageForUser(@current_user,@current_folder,params[:page],params[:sort_field],params[:sort_dir])
2011-08-02 23:12:17 +02:00
2011-07-27 20:34:30 +02:00
end
2011-07-29 20:05:47 +02:00
def compose
2011-09-16 19:44:29 +02:00
#before filter :prepare_compose_buttons, :create_message_with_params
2011-09-10 20:31:55 +02:00
@operation = :new
2011-09-16 19:44:29 +02:00
if params["cid"].present?
contact = @current_user.contacts.find_by_id(params["cid"])
if not contact.nil?
@message.to_addr = contact.email
end
elsif params["cids"].present?
contacts = []
params["cids"].each do |c|
contact = @current_user.contacts.find_by_id(c)
if not contact.nil?
contacts << contact.email
end
end
@message.to_addr = contacts.join(';')
end
2011-07-29 20:05:47 +02:00
end
2011-08-02 23:12:17 +02:00
def show
2011-09-03 13:07:40 +02:00
@images = []
@attachments = []
@text_part = nil
@html_part = nil
2011-08-24 19:20:13 +02:00
2011-08-26 22:59:43 +02:00
@message = @current_user.messages.find_by_uid(params[:id])
2011-08-16 20:05:58 +02:00
@message.update_attributes(:unseen => false)
2011-08-24 19:20:13 +02:00
imap_message = @mailbox.fetch_body(@message.uid)
2011-09-03 13:07:40 +02:00
mail = Mail.new(imap_message)
@plain_header = mail.header.to_s
2011-09-09 23:44:51 +02:00
2011-09-10 20:31:55 +02:00
# FIXME missing fields and support arrays
2011-09-10 13:49:42 +02:00
#@from = mail.From.addrs.presence
#@to = mail.To.addrs.presence
@from = @message.from_addr
@to = @message.to_addr
2011-09-09 23:44:51 +02:00
@cc = mail.Cc.presence
@bcc = mail.Bcc.presence
2011-09-09 22:10:25 +02:00
#@subject = mail.Subject
2011-09-09 23:44:51 +02:00
@date = mail.date.presence
2011-09-03 13:07:40 +02:00
if mail.multipart? == true
if not mail.text_part.nil?
@text_part = mail.text_part.decoded_and_charseted
end
if not mail.html_part.nil?
@html_part = mail.html_part.decoded_and_charseted
end
attachments = mail.attachments
if not attachments.size.zero?
for idx in 0..attachments.size - 1
a = attachments[idx]
a.idx = idx
a.parent_id = @message.uid
2011-09-10 11:23:10 +02:00
if a.isImage? and @current_user.prefs.msg_image_view_as.to_sym.eql?(:thumbnail)
2011-09-03 13:07:40 +02:00
@images << a
else
@attachments << a
end
end
end
2011-08-24 19:20:13 +02:00
else
2011-09-03 13:07:40 +02:00
part = Mail::Part.new(mail)
part.idx = 0
part.parent_id = @message.uid
if part.isText?
@text_part = part.decoded_and_charseted
2011-09-10 11:23:10 +02:00
elsif part.isImage? and @current_user.prefs.msg_image_view_as.to_sym.eql?(:thumbnail)
2011-09-03 13:07:40 +02:00
@images << part
elsif part.isHtml?
@html_part = part.decoded_and_charseted
else
@attachments << part
end
end
end
def html_body
message = @current_user.messages.find(params[:id])
mail = Mail.new(@mailbox.fetch_body(message.uid))
if mail.multipart?
@body = mail.html_part.decoded_and_charseted
else
@body = mail.decoded_and_charseted
end
2011-09-09 22:10:25 +02:00
2011-09-03 13:07:40 +02:00
if @body.nil?
@body = t(:no_body,:scope=>:message)
2011-09-09 22:10:25 +02:00
else
if @body=~/cid:([\w@\.]+)/
attachments = mail.attachments
if not attachments.size.zero?
for idx in 0..attachments.size - 1
@body.gsub!(/cid:#{attachments[idx].cid}/,messages_attachment_download_path(message.uid,idx))
end
end
end
2011-09-03 13:07:40 +02:00
end
render 'html_body',:layout => 'html_body'
2011-08-24 19:20:13 +02:00
end
def attachment
2011-09-05 19:08:22 +02:00
attachments = []
2011-08-27 00:10:45 +02:00
message = @current_user.messages.find(params[:id])
mail = Mail.new(@mailbox.fetch_body(message.uid))
2011-09-05 19:08:22 +02:00
if mail.multipart? == true
attachments = mail.attachments
2011-08-24 19:20:13 +02:00
else
2011-09-05 19:08:22 +02:00
attachments << Mail::Part.new(mail)
end
a = attachments[params[:idx].to_i]
headers['Content-type'] = a.main_type + "/" + a.sub_type
headers['Content-Disposition'] = %(attachment; filename="#{a.filename}")
render :text => a.decoded
2011-08-02 23:12:17 +02:00
end
2011-09-09 22:10:25 +02:00
2011-09-03 13:07:40 +02:00
############################################# protected section ##########################################
2011-09-05 19:08:22 +02:00
protected
2011-09-03 13:07:40 +02:00
2011-09-09 22:10:25 +02:00
def prepare_multi2_buttons
@multi2_buttons = []
2011-09-09 23:44:51 +02:00
@multi2_buttons << {:text => 'trash',:scope=>:message,:image => 'trash.png'}
2011-09-09 22:10:25 +02:00
@multi2_buttons << {:text => 'set_unread',:scope=>:message,:image => 'unseen.png'}
@multi2_buttons << {:text => 'set_read',:scope=>:message,:image => 'seen.png'}
end
def prepare_multi1_buttons
@multi1_buttons = []
@multi1_buttons << {:text => 'copy',:scope=>:message,:image => 'copy.png'}
@multi1_buttons << {:text => 'move',:scope=>:message,:image => 'move.png'}
end
def prepare_multi3_buttons
@multi3_buttons = []
@multi3_buttons << {:text => 'show_header',:scope=>:show,:image => 'zoom.png'}
2011-09-09 23:44:51 +02:00
@multi3_buttons << {:text => 'trash',:scope=>:show,:image => 'trash.png'}
2011-09-09 22:10:25 +02:00
@multi3_buttons << {:text => 'reply',:scope=>:show,:image => 'reply.png'}
end
2011-07-22 22:57:36 +02:00
end