From 961e9d6f4b1e83d860cfac9753f0f63f734d385b Mon Sep 17 00:00:00 2001 From: Wojciech Todryk Date: Sun, 28 Aug 2011 21:52:17 +0200 Subject: [PATCH] devel --- app/controllers/application_controller.rb | 35 ++++++++++++++++ app/models/attachment.rb | 51 +++++++++++++---------- app/models/message.rb | 42 +------------------ config/defaults.yml | 2 +- 4 files changed, 66 insertions(+), 64 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d6e2daf..d1bc6ad 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/models/attachment.rb b/app/models/attachment.rb index c7627fd..89417c8 100755 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb index df21096..31a1110 100755 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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, diff --git a/config/defaults.yml b/config/defaults.yml index 0183973..28b669a 100755 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -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'