lib/sup/message.rb in sup-0.2 vs lib/sup/message.rb in sup-0.3
- old
+ new
@@ -11,10 +11,14 @@
## 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.
+
class Message
SNIPPET_LEN = 80
RE_PATTERN = /^((re|re[\[\(]\d[\]\)]):\s*)+/i
## some utility methods
@@ -33,11 +37,11 @@
DEFAULT_SUBJECT = ""
DEFAULT_SENDER = "(missing sender)"
attr_reader :id, :date, :from, :subj, :refs, :replytos, :to, :source,
:cc, :bcc, :labels, :list_address, :recipient_email, :replyto,
- :source_info, :chunks
+ :source_info, :chunks, :list_subscribe, :list_unsubscribe
bool_reader :dirty, :source_marked_read
## if you specify a :header, will use values from that. otherwise,
## will try and load the header from the source.
@@ -54,20 +58,28 @@
end
def parse_header header
header.each { |k, v| header[k.downcase] = v }
- @from = PersonManager.person_for header["from"]
-
@id =
if header["message-id"]
sanitize_message_id header["message-id"]
else
- "sup-faked-" + Digest::MD5.hexdigest(raw_header)
- Redwood::log "faking message-id for message from #@from: #@id"
+ returning("sup-faked-" + Digest::MD5.hexdigest(raw_header)) do |id|
+ Redwood::log "faking message-id for message from #@from: #{id}"
+ end
end
+ @from =
+ if header["from"]
+ PersonManager.person_for header["from"]
+ else
+ name = "Sup Auto-generated Fake Sender <sup@fake.sender.example.com>"
+ Redwood::log "faking from for message #@id: #{name}"
+ PersonManager.person_for name
+ end
+
date = header["date"]
@date =
case date
when Time
date
@@ -97,10 +109,12 @@
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 snippet; @snippet || chunks && @snippet; end
def is_list_message?; !@list_address.nil?; end
@@ -157,10 +171,11 @@
message_to_chunks @source.load_message(@source_info)
rescue SourceError, SocketError, MessageFormatError => 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
[Chunk::Text.new(error_message(e.message))]
end
end
end
@@ -182,15 +197,18 @@
The error message was:
#{msg}
EOS
end
+ ## wrap any source methods that might throw sourceerrors
def with_source_errors_handled
begin
yield
rescue SourceError => e
Redwood::log "problem getting messages from #{@source}: #{e.message}"
+ @source.error ||= e
+ Redwood::report_broken_sources :force_to_top => true
error_message e.message
end
end
def raw_header
@@ -216,15 +234,15 @@
chunks.select { |c| c.is_a? Chunk::Text }.map { |c| c.lines },
Message.normalize_subj(subj),
].flatten.compact.join " "
end
- def basic_body_lines
- chunks.find_all { |c| c.is_a?(Chunk::Text) || c.is_a?(Chunk::Quote) }.map { |c| c.lines }.flatten
+ def quotable_body_lines
+ chunks.find_all { |c| c.quotable? }.map { |c| c.lines }.flatten
end
- def basic_header_lines
+ def quotable_header_lines
["From: #{@from.full_address}"] +
(@to.empty? ? [] : ["To: " + @to.map { |p| p.full_address }.join(", ")]) +
(@cc.empty? ? [] : ["Cc: " + @cc.map { |p| p.full_address }.join(", ")]) +
(@bcc.empty? ? [] : ["Bcc: " + @bcc.map { |p| p.full_address }.join(", ")]) +
["Date: #{@date.rfc822}",
@@ -332,15 +350,13 @@
from_person = from ? PersonManager.person_for(from.format) : nil
[Chunk::EnclosedMessage.new(from_person, payload.to_s)]
else
filename =
## first, paw through the headers looking for a filename
- if m.header["Content-Disposition"] &&
- m.header["Content-Disposition"] =~ /filename="?(.*?[^\\])("|;|$)/
+ if m.header["Content-Disposition"] && m.header["Content-Disposition"] =~ /filename="?(.*?[^\\])("|;|$)/
$1
- elsif m.header["Content-Type"] &&
- m.header["Content-Type"] =~ /name=(.*?)(;|$)/
+ elsif m.header["Content-Type"] && m.header["Content-Type"] =~ /name="?(.*?[^\\])("|;|$)/
$1
## haven't found one, but it's a non-text message. fake
## it.
elsif m.header["Content-Type"] && m.header["Content-Type"] !~ /^text\/plain/
@@ -358,14 +374,14 @@
end
end
end
def self.convert_from body, charset
- return body unless charset
-
begin
+ raise MessageFormatError, "RubyMail decode returned a null body" unless body
+ return body unless charset
Iconv.iconv($encoding, charset, body).join
- rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::IllegalSequence => e
+ 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("sup-unable-to-decode.txt", "w") { |f| f.write body }
body
end
end