lib/sup/message.rb in sup-0.7 vs lib/sup/message.rb in sup-0.8
- old
+ new
@@ -1,22 +1,19 @@
require 'time'
-require 'iconv'
module Redwood
-class MessageFormatError < StandardError; end
-
## a Message is what's threaded.
##
## it is also where the parsing for quotes and signatures is done, but
## that should be moved out to a separate class at some point (because
## i would like, for example, to be able to add in a ruby-talk
## specific module that would detect and link to /ruby-talk:\d+/
## sequences in the text of an email. (how sweet would that be?)
##
-## this class cathces all source exceptions. if the underlying source throws
-## an error, it is caught and handled.
+## this class catches all source exceptions. if the underlying source
+## throws an error, it is caught and handled.
class Message
SNIPPET_LEN = 80
RE_PATTERN = /^((re|re[\[\(]\d[\]\)]):\s*)+/i
@@ -48,91 +45,90 @@
@source = opts[:source] or raise ArgumentError, "source can't be nil"
@source_info = opts[:source_info] or raise ArgumentError, "source_info can't be nil"
@snippet = opts[:snippet]
@snippet_contains_encrypted_content = false
@have_snippet = !(opts[:snippet].nil? || opts[:snippet].empty?)
- @labels = [] + (opts[:labels] || [])
+ @labels = (opts[:labels] || []).to_set_of_symbols
@dirty = false
@encrypted = false
@chunks = nil
@attachments = []
## we need to initialize this. see comments in parse_header as to
## why.
@refs = []
- parse_header(opts[:header] || @source.load_header(@source_info))
+ #parse_header(opts[:header] || @source.load_header(@source_info))
end
def parse_header header
- header.keys.each { |k| header[k.downcase] = header[k] } # canonicalize
+ ## forcibly decode these headers from and to the current encoding,
+ ## which serves to strip out characters that aren't displayable
+ ## (and which would otherwise be screwing up the display)
+ %w(from to subject cc bcc).each do |f|
+ header[f] = Iconv.easy_decode($encoding, $encoding, header[f]) if header[f]
+ end
- fakeid = nil
- fakename = nil
+ @id = if header["message-id"]
+ mid = header["message-id"] =~ /<(.+?)>/ ? $1 : header["message-id"]
+ sanitize_message_id mid
+ else
+ id = "sup-faked-" + Digest::MD5.hexdigest(raw_header)
+ from = header["from"]
+ #Redwood::log "faking non-existent message-id for message from #{from}: #{id}"
+ id
+ end
- @id =
- if header["message-id"]
- sanitize_message_id header["message-id"]
- else
- fakeid = "sup-faked-" + Digest::MD5.hexdigest(raw_header)
- end
-
- @from =
- if header["from"]
- PersonManager.person_for header["from"]
- else
- fakename = "Sup Auto-generated Fake Sender <sup@fake.sender.example.com>"
- PersonManager.person_for fakename
- end
+ @from = Person.from_address(if header["from"]
+ header["from"]
+ else
+ name = "Sup Auto-generated Fake Sender <sup@fake.sender.example.com>"
+ #Redwood::log "faking non-existent sender for message #@id: #{name}"
+ name
+ end)
- Redwood::log "faking message-id for message from #@from: #{id}" if fakeid
- Redwood::log "faking from for message #@id: #{fakename}" if fakename
-
- date = header["date"]
- @date =
- case date
- when Time
- date
- when String
- begin
- Time.parse date
- rescue ArgumentError => e
- Redwood::log "faking date header for #{@id} due to error parsing date #{header['date'].inspect}: #{e.message}"
- Time.now
- end
- else
- Redwood::log "faking date header for #{@id}"
+ @date = case(date = header["date"])
+ when Time
+ date
+ when String
+ begin
+ Time.parse date
+ rescue ArgumentError => e
+ #Redwood::log "faking mangled date header for #{@id} (orig #{header['date'].inspect} gave error: #{e.message})"
Time.now
end
+ else
+ #Redwood::log "faking non-existent date header for #{@id}"
+ Time.now
+ end
@subj = header.member?("subject") ? header["subject"].gsub(/\s+/, " ").gsub(/\s+$/, "") : DEFAULT_SUBJECT
- @to = PersonManager.people_for header["to"]
- @cc = PersonManager.people_for header["cc"]
- @bcc = PersonManager.people_for header["bcc"]
+ @to = Person.from_address_list header["to"]
+ @cc = Person.from_address_list header["cc"]
+ @bcc = Person.from_address_list header["bcc"]
## before loading our full header from the source, we can actually
## have some extra refs set by the UI. (this happens when the user
## joins threads manually). so we will merge the current refs values
## in here.
refs = (header["references"] || "").scan(/<(.+?)>/).map { |x| sanitize_message_id x.first }
@refs = (@refs + refs).uniq
@replytos = (header["in-reply-to"] || "").scan(/<(.+?)>/).map { |x| sanitize_message_id x.first }
- @replyto = PersonManager.person_for header["reply-to"]
+ @replyto = Person.from_address header["reply-to"]
@list_address =
if header["list-post"]
- @list_address = PersonManager.person_for header["list-post"].gsub(/^<mailto:|>$/, "")
+ @list_address = Person.from_address header["list-post"].gsub(/^<mailto:|>$/, "")
else
nil
end
@recipient_email = header["envelope-to"] || header["x-original-to"] || header["delivered-to"]
@source_marked_read = header["status"] == "RO"
@list_subscribe = header["list-subscribe"]
@list_unsubscribe = header["list-unsubscribe"]
end
- private :parse_header
def add_ref ref
@refs << ref
@dirty = true
end
@@ -170,11 +166,11 @@
end
def has_label? t; @labels.member? t; end
def add_label t
return if @labels.member? t
- @labels.push t
+ @labels = (@labels + [t]).to_set_of_symbols
@dirty = true
end
def remove_label t
return unless @labels.member? t
@labels.delete t
@@ -184,11 +180,11 @@
def recipients
@to + @cc + @bcc
end
def labels= l
- @labels = l
+ @labels = l.to_set_of_symbols
@dirty = true
end
def chunks
load_from_source!
@@ -196,11 +192,11 @@
end
## this is called when the message body needs to actually be loaded.
def load_from_source!
@chunks ||=
- if @source.has_errors?
+ if @source.respond_to?(:has_errors?) && @source.has_errors?
[Chunk::Text.new(error_message(@source.error.message).split("\n"))]
else
begin
## we need to re-read the header because it contains information
## that we don't store in the index. actually i think it's just
@@ -210,11 +206,11 @@
## bloat the index.
## actually, it's also the differentiation between to/cc/bcc,
## so i will keep this.
parse_header @source.load_header(@source_info)
message_to_chunks @source.load_message(@source_info)
- rescue SourceError, SocketError, MessageFormatError => e
+ rescue SourceError, SocketError => e
Redwood::log "problem getting messages from #{@source}: #{e.message}"
## we need force_to_top here otherwise this window will cover
## up the error message one
@source.error ||= e
Redwood::report_broken_sources :force_to_top => true
@@ -370,10 +366,11 @@
decryptedm, sig, notice = CryptoManager.decrypt payload
children = message_to_chunks(decryptedm, true) if decryptedm
[notice, sig, children].flatten.compact
end
+ ## takes a RMail::Message, breaks it into Chunk:: classes.
def message_to_chunks m, encrypted=false, sibling_types=[]
if m.multipart?
chunks =
case m.header.content_type
when "multipart/signed"
@@ -389,11 +386,11 @@
chunks
elsif m.header.content_type == "message/rfc822"
payload = RMail::Parser.read(m.body)
from = payload.header.from.first
- from_person = from ? PersonManager.person_for(from.format) : nil
+ from_person = from ? Person.from_address(from.format) : nil
[Chunk::EnclosedMessage.new(from_person, payload.to_s)] +
message_to_chunks(payload, encrypted)
else
filename =
## first, paw through the headers looking for a filename
@@ -421,28 +418,20 @@
# add this to the attachments list if its not a generated html
# attachment (should we allow images with generated names?).
# Lowercase the filename because searches are easier that way
@attachments.push filename.downcase unless filename =~ /^sup-attachment-/
add_label :attachment unless filename =~ /^sup-attachment-/
- [Chunk::Attachment.new(m.header.content_type, filename, m, sibling_types)]
+ content_type = m.header.content_type || "application/unknown" # sometimes RubyMail gives us nil
+ [Chunk::Attachment.new(content_type, filename, m, sibling_types)]
## otherwise, it's body text
else
- body = Message.convert_from m.decode, m.charset if m.body
+ ## if there's no charset, use the current encoding as the charset.
+ ## this ensures that the body is normalized to avoid non-displayable
+ ## characters
+ body = Iconv.easy_decode($encoding, m.charset || $encoding, m.decode) if m.body
text_to_chunks((body || "").normalize_whitespace.split("\n"), encrypted)
end
- end
- end
-
- def self.convert_from body, charset
- begin
- raise MessageFormatError, "RubyMail decode returned a null body" unless body
- return body unless charset
- Iconv.easy_decode($encoding, charset, body)
- rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::IllegalSequence, MessageFormatError => e
- Redwood::log "warning: error (#{e.class.name}) decoding message body from #{charset}: #{e.message}"
- File.open(File.join(BASE_DIR,"unable-to-decode.txt"), "w") { |f| f.write body }
- body
end
end
## parse the lines of text into chunk objects. the heuristics here
## need tweaking in some nice manner. TODO: move these heuristics