This commit is contained in:
Wojciech Todryk 2011-08-28 21:52:17 +02:00
parent cead4d9d74
commit 961e9d6f4b
4 changed files with 66 additions and 64 deletions

View file

@ -57,6 +57,41 @@ class ApplicationController < ActionController::Base
@current_folder = @current_user.folders.find_by_full_name(@selected_folder)
end
def self.decode_quoted(text,unknown_charset = $defaults["msg_unknown_charset"])
begin
if text.=~(/=\?/).nil?
after = Iconv.conv('UTF-8',unknown_charset,text)
#after = text
else
# TODO support multiple showing of =?xxx?=
text =~ /(=\?.+\?=)/
after = text
match = $1
f = match.split(/\?/)
case f[2].downcase
when 'q':
replace = f[3].gsub(/_/," ").unpack("M").first
when 'b':
replace = f[3].gsub(/_/," ").unpack("m").first
else
replace = match
end
match.gsub!(/\?/,'\?')
match.gsub!(/\)/,'\)')
after = text.gsub(/#{match}/,replace)
if f[1].downcase != 'utf-8'
after = Iconv.conv('UTF-8',f[1],after)
end
end
return after
rescue Exception => e
logger.error("Class Message: #{e.to_s}: T: #{text} M: #{match} R: #{replace} A: #{after}")
return text
end
end
##################################### private section ##########################################
private

View file

@ -21,7 +21,7 @@ class Attachment
key = fields[0]
value = fields[1]
if Attachment.attribute_method?(key) == true
send("#{key}=", value)
send("#{key}=", ApplicationController.decode_quoted(value))
end
end
end
@ -33,23 +33,8 @@ class Attachment
end
def self.fromMultiParts(attachments,id,parts)
cid = ''
parts.each do |part|
if not part.content_id.nil?
part.content_id =~ /\<(\S+)\>/
cid = $1
else
cid = ''
end
a = Attachment.new( :message_id => id,
:description => part.content_description,
:type => part.content_type,
:content => part.body.raw_source,
:encoding => part.content_transfer_encoding,
:size => part.body.raw_source.size,
:multipart => part.multipart?,
:cid => cid
)
a = build(id,part)
if a.multipart?
fromMultiParts(attachments,id,part.parts)
else
@ -59,15 +44,29 @@ class Attachment
end
def self.fromSinglePart(attachments,id,part)
a = build(id,part)
attachments << a
end
def self.build(id,part)
cid = ''
if not part.content_id.nil?
part.content_id =~ /\<(\S+)\>/
cid = $1
else
cid = ''
end
a = Attachment.new( :message_id => id,
:description => part.content_description,
:type => part.content_type,
:encoding => part.body.encoding,
:content => part.body.raw_source,
:encoding => part.body.encoding,
:size => part.body.raw_source.size,
:charset => part.body.charset
:charset => part.body.charset,
:multipart => part.multipart?,
:cid => cid
)
attachments << a
return a
end
@ -137,7 +136,7 @@ class Attachment
end
def decode
begin
case @encoding
when /quoted-printable/
#decoded = @content.gsub(/_/," ").unpack("M").first
@ -157,20 +156,26 @@ class Attachment
else
decoded = @content
end
rescue
@content
end
end
def decode_and_charset
begin
decoded = decode
if not @charset == 'UTF-8'
@charset.nil? ? charset = $defaults["msg_unknown_encoding"] : charset = @charset
@charset.nil? ? charset = $defaults["msg_unknown_charset"] : charset = @charset
charseted = Iconv.iconv("UTF-8",charset,decoded).first
else
charseted = decoded
end
charseted
rescue
decoded
end
end

View file

@ -8,48 +8,10 @@ class Message < ActiveRecord::Base
set_primary_key :uid
attr_accessible :unseen, :to_addr, :size, :content_type, :folder_id, :subject, :date, :uid, :from_addr, :user_id, :msg_id
def self.decode(text,unknown_encoding = $defaults["msg_unknown_encoding"])
begin
if text.=~(/=\?/).nil?
after = Iconv.conv('UTF-8',unknown_encoding,text)
else
# TODO support multiple showing of =?xxx?=
text =~ /(=\?\S+\?=)/
after = text
match = $1
logger.custom('match',match)
f = match.split(/\?/)
case f[2].downcase
when 'q':
replace = f[3].gsub(/_/," ").unpack("M").first
when 'b':
replace = f[3].gsub(/_/," ").unpack("m").first
else
replace = match
end
logger.custom('replace',replace)
match.gsub!(/\?/,'\?')
match.gsub!(/\)/,'\)')
after = text.gsub(/#{match}/,replace)
logger.custom('after',after)
if f[1].downcase != 'utf-8'
after = Iconv.conv('UTF-8',f[1],after)
end
end
return after
rescue
logger.error("Class Message: String decode error: #{text}")
return text
end
end
def self.addr_to_db(addr)
ret = ""
name = addr.name
name.nil? ? ret : ret << decode(name)
name.nil? ? ret : ret << ApplicationController.decode_quoted(name)
ret << "<" + addr.mailbox + "@" + addr.host
ret
end
@ -74,7 +36,7 @@ class Message < ActiveRecord::Base
from = addr_to_db(envelope.from[0])
to = addr_to_db(envelope.to[0])
envelope.subject.nil? ? subject = "" : subject = decode(envelope.subject)
envelope.subject.nil? ? subject = "" : subject = ApplicationController.decode_quoted(envelope.subject)
create(
:user_id => user.id,

View file

@ -21,7 +21,7 @@ msg_address_length: 35
msg_search_fields: [subject, from, to]
# if encoding can not be get from data
msg_unknown_encoding: ISO-8859-2
msg_unknown_charset: ISO-8859-2
imap_debug: false
imap_use_ssl: 'false'