]> git.cworth.org Git - sup/blobdiff - lib/sup/message-chunks.rb
Merge branch 'master' into next
[sup] / lib / sup / message-chunks.rb
index 309577dc234260255d598c4f64c253138cd1d8b3..0606395052f4d7a4a467638da170019271f0df4c 100644 (file)
@@ -1,3 +1,5 @@
+require 'tempfile'
+
 ## Here we define all the "chunks" that a message is parsed
 ## into. Chunks are used by ThreadViewMode to render a message. Chunks
 ## are used for both MIME stuff like attachments, for Sup's parsing of
 ## included as quoted text during a reply. Text, Quotes, and mime-parsed
 ## attachments are quotable; Signatures are not.
 
+## monkey-patch time: make temp files have the right extension
+class Tempfile
+  def make_tmpname basename, n
+    sprintf '%d-%d-%s', $$, n, basename
+  end
+end
+
+
 module Redwood
 module Chunk
   class Attachment
@@ -36,14 +46,23 @@ module Chunk
 Executes when decoding a MIME attachment.
 Variables:
    content_type: the content-type of the message
-       filename: the filename of the attachment as saved to disk (generated
-                 on the fly, so don't call more than once)
+       filename: the filename of the attachment as saved to disk
   sibling_types: if this attachment is part of a multipart MIME attachment,
                  an array of content-types for all attachments. Otherwise,
                  the empty array.
 Return value:
   The decoded text of the attachment, or nil if not decoded.
 EOS
+
+    HookManager.register "mime-view", <<EOS
+Executes when viewing a MIME attachment, i.e., launching a separate
+viewer program.
+Variables:
+   content_type: the content-type of the attachment
+       filename: the filename of the attachment as saved to disk
+Return value:
+  True if the viewing was successful, false otherwise.
+EOS
 #' stupid ruby-mode
 
     ## raw_content is the post-MIME-decode content. this is used for
@@ -54,7 +73,8 @@ EOS
     def initialize content_type, filename, encoded_content, sibling_types
       @content_type = content_type
       @filename = filename
-      @quotable = false # only quotable if we can parse it through the mime-decode hook
+      @quotable = false # changed to true if we can parse it through the
+                        # mime-decode hook, or if it's plain text
       @raw_content =
         if encoded_content.body
           encoded_content.decode
@@ -62,19 +82,21 @@ EOS
           "For some bizarre reason, RubyMail was unable to parse this attachment.\n"
         end
 
-      @lines =
+      text =
         case @content_type
         when /^text\/plain\b/
-          Message.convert_from(@raw_content, encoded_content.charset).split("\n")
+          Message.convert_from @raw_content, encoded_content.charset
         else
-          text = HookManager.run "mime-decode", :content_type => content_type,
-                                 :filename => lambda { write_to_disk },
-                                 :sibling_types => sibling_types
-          if text
-            @quotable = true
-            text.split("\n")
-          end
+          HookManager.run "mime-decode", :content_type => content_type,
+                          :filename => lambda { write_to_disk },
+                          :sibling_types => sibling_types
         end
+
+      @lines = nil
+      if text
+        @lines = text.gsub("\r\n", "\n").gsub(/\t/, "        ").gsub(/\r/, "").split("\n")
+        @quotable = true
+      end
     end
 
     def color; :none end
@@ -93,14 +115,22 @@ EOS
     def expandable?; !viewable? end
     def initial_state; :open end
     def viewable?; @lines.nil? end
+    def view_default! path
+      cmd = "/usr/bin/run-mailcap --action=view '#{@content_type}:#{path}' > /dev/null 2> /dev/null"
+      Redwood::log "running: #{cmd.inspect}"
+      system cmd
+      $? == 0
+    end
+
     def view!
       path = write_to_disk
-      system "/usr/bin/run-mailcap --action=view #{@content_type}:#{path} > /dev/null 2> /dev/null"
-      $? == 0
+      ret = HookManager.run "mime-view", :content_type => @content_type,
+                                         :filename => path
+      view_default! path unless ret
     end
 
     def write_to_disk
-      file = Tempfile.new "redwood.attachment"
+      file = Tempfile.new(@filename || "sup-attachment")
       file.print @raw_content
       file.close
       file.path