]> git.notmuchmail.org Git - sup/blobdiff - lib/sup/message.rb
many many changes. this is what happens when i have 5 hours on an airplane
[sup] / lib / sup / message.rb
index 963805b9893202a364ebfa5d453075ccf082f473..8ec8a673730312d5942e2512184d32a001d3481c 100644 (file)
@@ -13,6 +13,10 @@ class MessageFormatError < StandardError; end
 ## 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
@@ -35,7 +39,7 @@ class Message
 
   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
 
@@ -55,14 +59,24 @@ class Message
 
   def parse_header header
     header.each { |k, v| header[k.downcase] = v }
-
-    @from = PersonManager.person_for header["from"]
-
-    @id = header["message-id"]
-    unless @id
-      @id = "sup-faked-" + Digest::MD5.hexdigest(raw_header)
-      Redwood::log "faking message-id for message from #@from: #@id"
-    end
+    
+    @id =
+      if header["message-id"]
+        sanitize_message_id header["message-id"]
+      else
+        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 =
@@ -84,8 +98,9 @@ class Message
     @to = PersonManager.people_for header["to"]
     @cc = PersonManager.people_for header["cc"]
     @bcc = PersonManager.people_for header["bcc"]
-    @refs = (header["references"] || "").gsub(/[<>]/, "").split(/\s+/).flatten
-    @replytos = (header["in-reply-to"] || "").scan(/<(.*?)>/).flatten
+    @refs = (header["references"] || "").scan(/<(.+?)>/).map { |x| sanitize_message_id x.first }
+    @replytos = (header["in-reply-to"] || "").scan(/<(.+?)>/).map { |x| sanitize_message_id x.first }
+
     @replyto = PersonManager.person_for header["reply-to"]
     @list_address =
       if header["list-post"]
@@ -96,6 +111,8 @@ class Message
 
     @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
 
@@ -107,6 +124,8 @@ class Message
     @source.fn_for_offset @source_info
   end
 
+  def sanitize_message_id mid; mid.gsub(/\s/, "") end
+
   def save index
     index.sync_message self if @dirty
     @dirty = false
@@ -154,6 +173,7 @@ class Message
           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
@@ -179,11 +199,14 @@ The error message was:
 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
@@ -213,11 +236,11 @@ EOS
     ].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(", ")]) +
@@ -274,7 +297,7 @@ private
       return
     end
 
-    [CryptoManager.verify(payload, signature), message_to_chunks(payload)]
+    [CryptoManager.verify(payload, signature), message_to_chunks(payload)].flatten.compact
   end
 
   def multipart_encrypted_to_chunks m
@@ -321,6 +344,11 @@ private
       end
 
       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
+      [Chunk::EnclosedMessage.new(from_person, payload.to_s)]
     else
       filename =
         ## first, paw through the headers looking for a filename
@@ -350,11 +378,11 @@ private
   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