This commit is contained in:
Wojciech Todryk 2011-08-24 19:20:13 +02:00
parent 081bf95ce7
commit b9561278d0
53 changed files with 2893 additions and 9554 deletions

View file

@ -2,15 +2,16 @@ require 'yaml'
class ApplicationController < ActionController::Base
#unless config.consider_all_requests_local
# #rescue_from ActionController::RoutingError, :with => :route_not_found
# rescue_from ActiveRecord::RecordNotFound, :with => :route_not_found
#end
protect_from_forgery
before_filter :load_defaults,:current_user,:set_locale
before_filter :plugins_configuration
rescue_from ActiveRecord::RecordNotFound do
logger.custom('record_not_found','exc')
reset_session
redirect_to :controller=>'user', :action => 'login'
end
################################# protected section ###########################################
protected
@ -42,7 +43,7 @@ class ApplicationController < ActionController::Base
def check_current_user
if @current_user.nil?
session["return_to"] = request.fullpath
redirect_to :controller=>"user", :action => "login"
redirect_to :controller => 'user', :action => 'login'
return false
end
end
@ -65,9 +66,5 @@ class ApplicationController < ActionController::Base
WillPaginate::ViewHelpers.pagination_options[:next_label] = t(:next_page)
end
#def route_not_found
# render :text => 'What the fuck are you looking for ?', :status => :not_found
#end
end

View file

@ -8,8 +8,8 @@ class FoldersController < ApplicationController
before_filter :check_current_user ,:selected_folder
before_filter :open_imap_session, :except => [:index,:refresh_status,:show_hide]
after_filter :close_imap_session, :except => [:index,:refresh_status,:show_hide]
before_filter :open_imap_session, :except => [:index,:show_hide]
after_filter :close_imap_session, :except => [:index,:show_hide]
before_filter :get_folders
@ -25,6 +25,7 @@ class FoldersController < ApplicationController
render "index"
else
begin
#TODO recreate local copy of folders
if params["parent_folder"].empty?
@mailbox.create_folder(params[:folder])
else
@ -39,10 +40,11 @@ class FoldersController < ApplicationController
render 'index'
return
end
redirect_to :action => 'refresh', :flash => t(:was_created,:scope=>:folder), :type => :notice
flash[:notice] = t(:was_created,:scope=>:folder)
redirect_to :action => 'index'
end
end
# FIXME if you delete folder you should change current folder because if you go to messages/index you got nil
def delete
if params["folder"].empty?
flash[:warning] = t(:to_delete_empty,:scope=>:folder)
@ -59,25 +61,33 @@ class FoldersController < ApplicationController
raise Exception, t(:system_folder)
end
@mailbox.delete_folder(folder.full_name)
logger.custom('c',@current_folder.inspect)
logger.custom('f',folder.inspect)
if @current_folder.eql? folder
session[:selected_folder] = $defaults['mailbox_inbox']
end
folder.destroy
rescue Exception => e
flash[:error] = t(:can_not_delete,:scope=>:folder) + ' (' + e.to_s + ')'
render 'index'
return
end
redirect_to :action => 'refresh', :flash => t(:was_deleted,:scope=>:folder), :type => :notice
flash[:notice] = t(:was_deleted,:scope=>:folder)
redirect_to :action => 'index'
end
end
def show_hide
@folders.each do |f|
logger.info f.inspect,"\n"
if params["folders_to_show"].include?(f.id.to_s)
f.shown = true
f.save
else
f.shown = false
f.save
end
if !params["folders_to_show"].nil?
@folders.each do |f|
if params["folders_to_show"].include?(f.id.to_s)
f.shown = true
f.save
else
f.shown = false
f.save
end
end
end
redirect_to :action => 'index'
end
@ -88,6 +98,39 @@ class FoldersController < ApplicationController
redirect_to :action => 'index'
end
def select
session[:selected_folder] = params[:id]
redirect_to :controller => 'messages', :action => 'index'
end
def refresh_status
@folders_shown.each do |f|
@mailbox.set_folder(f.full_name)
folder_status = @mailbox.status
f.update_attributes(:total => folder_status['MESSAGES'], :unseen => folder_status['UNSEEN'])
end
redirect_to :controller=> 'messages', :action => 'index'
end
def emptybin
begin
trash_folder = @current_user.folders.find_by_full_name($defaults["mailbox_trash"])
@mailbox.set_folder(trash_folder.full_name)
trash_folder.messages.each do |m|
@mailbox.delete_message(m.uid)
end
@mailbox.expunge
trash_folder.messages.destroy_all
trash_folder.update_attributes(:unseen => 0, :total => 0)
rescue Exception => e
flash[:error] = "#{t(:imap_error)} (#{e.to_s})"
end
redirect_to :controller => 'messages', :action => 'index'
end
############################################# protected section #######################################
protected
def get_folders

View file

@ -28,4 +28,11 @@ class InternalController < ApplicationController
redirect_to :controller=>'user', :action => 'login'
end
def onlycanlogins
reset_session
flash[:error] = t(:only_can_logins,:scope=>:user)
@current_user = nil
redirect_to :controller=>'user', :action => 'login'
end
end

View file

@ -1,6 +1,7 @@
require 'imap_session'
require 'imap_mailbox'
require 'imap_message'
require 'mail'
class MessagesController < ApplicationController
@ -9,18 +10,20 @@ class MessagesController < ApplicationController
include ImapMessageModule
include MessagesHelper
before_filter :check_current_user ,:selected_folder
before_filter :get_current_folders
before_filter :check_current_user ,:selected_folder,:get_current_folders
before_filter :open_imap_session, :select_imap_folder
after_filter :close_imap_session
theme :theme_resolver
def index
if @current_folder.nil?
redirect_to :controller => 'folders', :action => 'index'
return
end
@messages = []
folder_status = @mailbox.status
@ -45,53 +48,38 @@ class MessagesController < ApplicationController
end
def folder
session[:selected_folder] = params[:id]
redirect_to :action => 'index'
end
def compose
flash[:notice] = 'Not impelented yet'
@message = Message.new
end
# TODO error when no folders are shown
def reply
@message = Message.new
render 'compose'
end
def refresh
@folders_shown.each do |f|
@mailbox.set_folder(f.full_name)
folder_status = @mailbox.status
f.update_attributes(:total => folder_status['MESSAGES'], :unseen => folder_status['UNSEEN'])
end
def sendout
flash[:notice] = t(:was_sent,:scope => :sendout)
redirect_to :action => 'index'
end
end
def emptybin
def msgops
begin
trash_folder = @current_user.folders.find_by_full_name($defaults["mailbox_trash"])
@mailbox.set_folder(trash_folder.full_name)
trash_folder.messages.each do |m|
logger.custom('id',m.inspect)
@mailbox.delete_message(m.uid)
if !params["uids"]
flash[:warning] = t(:no_selected,:scope=>:message)
elsif params["reply"]
redirect_to :action => 'reply', :id => params[:id]
return
end
@mailbox.expunge
trash_folder.messages.destroy_all
trash_folder.update_attributes(:unseen => 0, :total => 0)
rescue Exception => e
flash[:error] = "#{t(:imap_error)} (#{e.to_s})"
end
redirect_to :action => 'index'
end
redirect_to :action => 'show', :id => params[:id]
end
def ops
begin
if !params["uids"]
flash[:warning] = t(:no_selected,:scope=>:message)
elsif params["delete"]
params["uids"].each do |uid|
@mailbox.delete_message(uid)
@current_user.messages.find_by_uid(uid).destroy
end
@current_folder.update_stats
elsif params["set_unread"]
params["uids"].each do |uid|
@mailbox.set_unread(uid)
@ -116,7 +104,7 @@ class MessagesController < ApplicationController
if params["dest_folder"].empty?
flash[:warning] = t(:no_selected,:scope=>:folder)
else
dest_folder = find(params["dest_folder"])
dest_folder = @current_user.folders.find(params["dest_folder"])
params["uids"].each do |uid|
@mailbox.copy_message(uid,dest_folder.full_name)
message = @current_folder.messages.find_by_uid(uid)
@ -149,10 +137,86 @@ class MessagesController < ApplicationController
end
def show
@attachments = []
@render_as_text = []
@message = @current_user.messages.find(params[:id])
@message.update_attributes(:unseen => false)
flash[:notice] = 'Not implemented yet'
@body = @mailbox.fetch_body(@message.uid)
imap_message = @mailbox.fetch_body(@message.uid)
parts = imap_message.split(/\r\n\r\n/)
@message_header = parts[0]
mail = Mail.new(imap_message)
@title = mail.subject
if mail.multipart?
idx = 0
mail.parts.each do |part|
case part.content_type
when /^text\/plain/ then
@render_as_text << part.body.raw_source
else
a = Attachment.new( :message_id => @message.id,
:description => part.content_description,
:type => part.content_type,
:content => part.body.raw_source,
:idx => idx
)
@attachments << a
end
idx += 1
end
else
if mail.content_type.nil?
@render_as_text << mail.body.raw_source
else
a = Attachment.new( :message_id => @message.id,
:type => mail.content_type,
:encoding => mail.body.encoding,
:charset => mail.body.charset,
:content => mail.body.raw_source,
:idx => 0
)
@attachments << a
end
end
end
def body
message = @mailbox.fetch_body(params[:id].to_i)
mail = Mail.new(message)
@title = ''
@body = ''
#
#header = parts[0]
#body = parts[1]
#@body = "<html><head><title>ala</title><body><pre>#{header}</pre>#{mail.inspect}</body></html>"
render 'mail_view',:layout => 'mail_view'
end
def attachment
@message = @current_user.messages.find(params[:id])
mail = Mail.new(@mailbox.fetch_body(@message.uid))
if mail.multipart?
part = mail.parts[params[:idx].to_i]
a = Attachment.new( :message_id => @message.id,
:description => part.content_description,
:type => part.content_type,
:content => part.body.raw_source,
:idx => params[:idx]
)
else
a = Attachment.new( :message_id => @message.id,
:type => mail.content_type,
:encoding => mail.body.encoding,
:charset => mail.body.charset,
:content => mail.body.raw_source,
:idx => 0
)
end
headers['Content-type'] = a.type
headers['Content-Disposition'] = %(attachment; filename="#{a.name}")
render :text => a.content_decoded
end
end

View file

@ -12,9 +12,11 @@ class UserController < ApplicationController
redirect_to :action => "login"
end
# TODO make login possible to use only one username
def authenticate
if !$defaults["only_can_logins"].include?(params[:user][:email])
redirect_to :controller => 'internal', :action => 'onlycanlogins'
return false
end
user = User.find_by_email(params[:user][:email])
if user.nil?
redirect_to :action => 'unknown' ,:email=> params[:user][:email]
@ -26,7 +28,7 @@ class UserController < ApplicationController
redirect_to(session["return_to"])
session["return_to"] = nil
else
redirect_to :controller=> 'messages', :action=> 'refresh'
redirect_to :controller=> 'messages', :action=> 'index'
end
end

View file

@ -4,18 +4,16 @@ def form_field(object,field,flabel,example,val)
model_name = eval(object.class.model_name)
html = ""
html << "<div class=\"group\">"
if object.errors[field.to_sym]
if not object.errors[field.to_sym].empty?
html << "<div class=\"fieldWithErrors\">"
end
html << "<label class=\"label\">"
if flabel.nil?
html << model_name.human_attribute_name(field)
else
html << t(flabel.to_sym)
end
flabel.nil? ? html << model_name.human_attribute_name(field) : html << t(flabel.to_sym)
html << "</label>"
if object.errors[field.to_sym]
if not object.errors[field.to_sym].empty?
html << "<span class=\"error\"> "
html << object.errors[field.to_sym].to_s
html << "</span>"
@ -25,8 +23,8 @@ def form_field(object,field,flabel,example,val)
html << object.class.name.downcase+"_"+field
html << "\""
html << " name=\"#{object.class.name.downcase}[#{field}]\""
html << " size=50 type=\"text\" class=\"text_field\" value=\""
value = object.instance_eval(field) || val || ""
html << " type=\"text\" class=\"text_field\" value=\""
value = object.instance_eval(field) || val || ""
html << value
html << "\"/>"
html << "<span class=\"description\">"
@ -38,6 +36,47 @@ def form_field(object,field,flabel,example,val)
end
def mail_param_view(object,field,value)
model_name = eval(object.class.model_name)
html = ""
html << "<div class=\"group\">"
html << "<label class=\"label\">#{model_name.human_attribute_name(field)}: </label>"
html << value
html << "</div>"
html
end
def area_field(object,field,flabel,example,val,cols,rows)
model_name = eval(object.class.model_name)
html = ""
html << "<div class=\"group\">"
if not object.errors[field.to_sym].empty?
html << "<div class=\"fieldWithErrors\">"
end
html << "<label class=\"label\">"
flabel.nil? ? html << model_name.human_attribute_name(field) : html << t(flabel.to_sym)
html << "</label>"
if not object.errors[field.to_sym].empty?
html << "<span class=\"error\">"
html << object.errors[field.to_sym].to_s
html << "</span>"
html << "</div>"
end
name = object.class.name.downcase + '[' + field + ']'
id = object.class.name.downcase+"_"+field
value = object.instance_eval(field) || val || ""
html << "<textarea id=\"#{id}\" name=\"#{name}\" class=\"text_area\" cols=\"#{cols}\" rows=\"#{rows}\" value=\"#{value}\"></textarea>"
desc = t(:example) + ": " + example
html << "<span class=\"description\">#{desc}</span>"
html << "</div>"
end
def form_button(text,image)
html = ""
html << "<div class=\"group navform wat-cf\">"

View file

@ -3,12 +3,12 @@ module FolderHelper
def folder_link(folder)
folder.parent.empty? ? name = folder.name : name = folder.parent.gsub(/\./,'#') + "#" + folder.name
s = link_to folder.name.capitalize, messages_folder_path(:id => name)
s = link_to folder.name.capitalize, folders_select_path(:id => name)
if folder.full_name.downcase == $defaults["mailbox_trash"].downcase
if not folder.total.zero?
s <<' ('
s << link_to(t(:emptybin,:scope=>:folder),messages_emptybin_path)
s << link_to(t(:emptybin,:scope=>:folder),folders_emptybin_path)
s << ')'
end
else

View file

@ -11,13 +11,20 @@ module MessagesHelper
end
def date_formatter(date)
date.strftime("%Y-%m-%d %H:%M")
date.nil? ? t(:no_data) : date.strftime("%Y-%m-%d %H:%M")
end
def address_formatter(addr)
ImapMessageModule::IMAPAddress.parse(addr).friendly
end
def show_address_formatter(addr)
html = ""
fs = addr.split(/#/)
fs[0].size.zero? ? html << h("<")+fs[1]+h("@")+fs[2]+h(">") : html << fs[0]+h(" <")+fs[1]+h("@")+fs[2]+h(">")
html
end
def subject_formatter(message)
if message.subject.size.zero?
s = t(:no_subject,:scope=>:message)
@ -25,7 +32,7 @@ module MessagesHelper
length = $defaults["msg_subject_length"].to_i
message.subject.length >= length ? s = message.subject[0,length]+"..." : s = message.subject
end
link_to s,:controller => 'messages', :action => 'show', :id => message.id
link_to s,{:controller => 'messages', :action => 'show', :id => message.id} , :title => message.subject
end
def attachment_formatter(message)
@ -53,5 +60,11 @@ module MessagesHelper
html
end
def content_text_plain_for_render(text)
html = h(text)
html.gsub!(/\r\n/,"<br/>")
html
end
end

89
app/models/attachment.rb Executable file
View file

@ -0,0 +1,89 @@
class Attachment
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :type, :charset, :encoding, :name, :description, :content, :message_id, :idx
attr_reader :link
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
if @type =~ /name=(\S+)/
@name = $1
@type =~ /^(\S+);/
@type = $1
else
@name = "no_name.dat"
end
end
def persisted?
false
end
def to_s
s = "Attachment:\n"
instance_variables.sort.each do |name|
if name == "@content"
s += "\t#{name}: size #{instance_variable_get(name).size}\n"
else
s += "\t#{name}: #{instance_variable_get(name)}\n"
end
end
s
end
def title
@description.nil? ? @name : @description
end
def type
@type.nil? ? '' : @type
end
def charset
@charset.nil? ? '' : @charset
end
def encoding
@encoding.nil? ? '' : @encoding
end
# def to_html
# html = "<span class=\"attachment\">"
# html << "<span class=\"title\">"
# html << "<a href=\"/messages/attachment/#{@message_id}/#{@idx}\">#{@description.nil? ? @name : @description}</a>"
# html << "</span> #{@type}"
# @charset.nil? ? html : html << " #{@charset}"
# @encoding.nil? ? html : html << " #{@encoding}"
# case @type
# when /^message\/delivery-status/
# html << "<pre>"
# html << @content
# html << "</pre>"
# #when /^message\/rfc822/
# # html << "<pre>"
# # html << @content
# # html << "</pre>"
# end
# html << "</span>"
# end
# def to_table
# html = ""
## @type.nil? ? html << "<td></td>" : html << "<td>#{@type}</td>"
# @charset.nil? ? html << "<td></td>" : html << "<td>#{@charset}</td>"
# @encoding.nil? ? html << "<td></td>" : html << "<td>#{@encoding}</td>"
# html
# end
def content_decoded
# TODO attachments decoding
@content
end
end

View file

@ -40,6 +40,9 @@ class Folder < ActiveRecord::Base
def check_fill_params
self.total.nil? ? self.total = 0 : self.total
self.unseen.nil? ? self.unseen = 0 : self.unseen
self.parent.nil? ? self.parent = "" : self.parent
self.haschildren.nil? ? self.haschildren = false : self.haschildren
self.delim.nil? ? self.delim = "." : self.delim
end
def self.createBulk(user,imapFolders)

View file

@ -2,6 +2,8 @@ class Message < ActiveRecord::Base
belongs_to :user
belongs_to :folder
attr_accessor :body
def self.getPageForUser(user,folder,page,sort_field,sort_dir)
order = 'date desc'
@ -22,7 +24,7 @@ class Message < ActiveRecord::Base
:msg_id => mess.message_id,
:uid => mess.uid,
:from_addr => mess.from_to_db,
:to_addr => mess.to,
:to_addr => mess.to_to_db,
:subject => mess.subject,
:content_type => mess.content_type,
:date => mess.date,