mailr/app/controllers/messages_controller.rb

165 lines
5.1 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-07-31 22:45:29 +02:00
2011-08-16 20:05:58 +02:00
before_filter :open_imap_session, :select_imap_folder
2011-09-03 13:07:40 +02:00
2011-09-05 19:08:22 +02:00
before_filter :prepare_compose_buttons, :only => [:compose]
2011-09-03 13:07:40 +02:00
#before_filter :mail_defaults, :only => [:sendout_or_save]
2011-09-05 19:08:22 +02:00
before_filter :get_system_folders, :only => [:index]
2011-09-03 13:07:40 +02:00
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-08-24 19:20:13 +02:00
@message = Message.new
2011-09-03 13:07:40 +02:00
if params[:message]
@message = update_attributes(params[:message])
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
@from = mail.From.addrs
@to = mail.To.addrs
@cc = mail.Cc
@bcc = mail.Bcc
@subject = mail.Subject
@date = mail.date
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
if a.isImage?
@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
elsif part.isImage?
@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
if @body.nil?
@body = t(:no_body,:scope=>:message)
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-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-07-22 22:57:36 +02:00
end