diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb
index c3bb144..28afba9 100755
--- a/app/controllers/messages_controller.rb
+++ b/app/controllers/messages_controller.rb
@@ -39,8 +39,7 @@ class MessagesController < ApplicationController
(uids_remote-uids_local).each_slice($defaults["imap_fetch_slice"].to_i) do |slice|
messages = @mailbox.uid_fetch(slice, ImapMessageModule::IMAPMessage.fetch_attr)
messages.each do |m|
- mess = ImapMessageModule::IMAPMessage.fromImap(m)
- Message.createForUser(@current_user,@current_folder,mess)
+ Message.createForUser(@current_user,@current_folder,m)
end
end
@@ -141,41 +140,50 @@ class MessagesController < ApplicationController
@attachments = []
@render_as_text = []
- @message = @current_user.messages.find(params[:id])
+ @message = @current_user.messages.find_by_uid(params[:id])
@message.update_attributes(:unseen => false)
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
+ @mail = Mail.new(imap_message)
+ if @mail.multipart?
+# idx = 0
+# @mail.parts.each do |part|
+# a = Attachment.new( :message_id => @message.id,
+# :description => part.content_description,
+# :type => part.content_type,
+# :content => part.body.raw_source,
+# :encoding => part.content_transfer_encoding,
+# :idx => idx,
+# :multipart => part.multipart?
+# )
+# logger.custom('a',a.to_s)
+# if a.isText?
+# @render_as_text << a.content_normalized
+# else
+# @attachments << a
+# end
+#
+# idx += 1
+# end
+ Attachment.fromPart(@attachments,@message.id,@mail.parts,0)
+ @attachments.each do |a|
+ a.isText? ? @render_as_text << a.content_normalized : @render_as_text
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,
+ a = Attachment.new( :message_id => @message.id,
+ :description => @mail.content_description,
+ :type => @mail.content_type,
+ :encoding => @mail.body.encoding,
+ :charset => @mail.body.charset,
+ :content => @mail.body.raw_source,
:idx => 0
)
+ logger.custom('a',a.to_s)
+ if a.isText?
+ @render_as_text << a.content_normalized
+ else
@attachments << a
end
end
@@ -203,6 +211,7 @@ class MessagesController < ApplicationController
:description => part.content_description,
:type => part.content_type,
:content => part.body.raw_source,
+ :encoding => part.content_transfer_encoding,
:idx => params[:idx]
)
else
@@ -216,7 +225,7 @@ class MessagesController < ApplicationController
end
headers['Content-type'] = a.type
headers['Content-Disposition'] = %(attachment; filename="#{a.name}")
- render :text => a.content_decoded
+ render :text => a.decode
end
end
diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb
index 5304635..42b2497 100755
--- a/app/controllers/user_controller.rb
+++ b/app/controllers/user_controller.rb
@@ -13,10 +13,14 @@ class UserController < ApplicationController
end
def authenticate
- if !$defaults["only_can_logins"].include?(params[:user][:email])
- redirect_to :controller => 'internal', :action => 'onlycanlogins'
- return false
+
+ if not $defaults["only_can_logins"].nil?
+ if not $defaults["only_can_logins"].include?(params[:user][:email])
+ redirect_to :controller => 'internal', :action => 'onlycanlogins'
+ return false
+ end
end
+
user = User.find_by_email(params[:user][:email])
if user.nil?
redirect_to :action => 'unknown' ,:email=> params[:user][:email]
diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb
index bfcdbc3..11eaf5a 100755
--- a/app/helpers/messages_helper.rb
+++ b/app/helpers/messages_helper.rb
@@ -14,15 +14,24 @@ module MessagesHelper
date.nil? ? t(:no_data) : date.strftime("%Y-%m-%d %H:%M")
end
- def address_formatter(addr)
- ImapMessageModule::IMAPAddress.parse(addr).friendly
- end
+# def address_formatter(addr)
+# ImapMessageModule::IMAPAddress.parse(addr).friendly
+# end
+
+ def address_formatter(addr,mode)
+ s = ""
+ length = $defaults["msg_address_length"].to_i
+ fs = addr.split(/)
+
+ if mode == :index
+ fs[0].size.zero? ? s = fs[1] : s = fs[0]
+ s.length >= length ? s = s[0,length]+"..." : s
+ else
+ fs[0].size.zero? ? s = "<" + fs[1] + ">" : s << fs[0] + " <" + fs[1] + ">"
+ end
+
+ return h(s)
- 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)
@@ -32,7 +41,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} , :title => message.subject
+ link_to s,{:controller => 'messages', :action => 'show', :id => message.uid} , :title => message.subject
end
def attachment_formatter(message)
@@ -61,8 +70,9 @@ module MessagesHelper
end
def content_text_plain_for_render(text)
- html = h(text)
- html.gsub!(/\r\n/,"
")
+ html = "
" + html << h(text) + html << "" html end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 60cb816..affcefb 100755 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -3,20 +3,51 @@ class Attachment include ActiveModel::Conversion extend ActiveModel::Naming - attr_accessor :type, :charset, :encoding, :name, :description, :content, :message_id, :idx + attr_accessor :type, :charset, :encoding, :name, :description, :content, :message_id, :idx, :boundary, :format, :multipart 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" + if not @type.nil? + params = @type.split(/;\s*/) + @type = params[0] + params.each do |p| + if p =~ /=/ + fields = p.split(/=/) + key = fields[0] + value = fields[1] + if Attachment.attribute_method?(key) == true + send("#{key}=", value) + end + end + end + end + end + + def multipart? + multipart + end + + def self.fromPart(attachments,id,parts,idx) + parts.each do |part| + a = Attachment.new( :message_id => id, + :description => part.content_description, + :type => part.content_type, + :content => part.body.raw_source, + :encoding => part.content_transfer_encoding, + :idx => idx, + :multipart => part.multipart? + ) + if a.multipart? + fromPart(attachments,id,part.parts,idx) + else + attachments << a + end + idx += 1 end end @@ -24,6 +55,25 @@ class Attachment false end + + + def name + if @name.nil? + case type + when /^text\/html/ + "index#{idx}.html" + when /^multipart/ + "multipart#{idx}.part" + when /^text\/plain/ + "file#{idx}.txt" + else + "filaname.dat" + end + else + @name + end + end + def to_s s = "Attachment:\n" instance_variables.sort.each do |name| @@ -36,8 +86,12 @@ class Attachment s end + def isText? + @type.nil? or @type =~ /^text\/plain/ + end + def title - @description.nil? ? @name : @description + @description.nil? ? name : @description end def type @@ -52,6 +106,51 @@ class Attachment @encoding.nil? ? '' : @encoding end + def format + @format.nil? ? '' : @format + end + + def boundary + @boundary.nil? ? '' : @boundary + end + + def decode + case @encoding + when /quoted-printable/ + decoded = @content.gsub(/_/," ").unpack("M").first + when /base64/ + decoded = @content.unpack("m").first + when /uuencode/ + array = @content.split(/\n/) + if array[0] =~ /^begin/ + size = array.size + array.delete_at(size-1) + array.delete_at(size-2) + array.delete_at(0) + end + string = array.join + decoded = string.unpack("u").first + else + decoded = @content + end + end + + def content_normalized + + decoded = decode + + if not @charset == 'UTF-8' + @charset.nil? ? charset = $defaults["msg_unknown_encoding"] : charset = @charset + charseted = Iconv.iconv("UTF-8",charset,decoded) + else + charseted = decoded + end + + charseted + + end + +end # def to_html # html = "