]> git.cworth.org Git - sup/blobdiff - lib/sup/message-chunks.rb
Merge branch 'run-mailcap-fixes'
[sup] / lib / sup / message-chunks.rb
index 309577dc234260255d598c4f64c253138cd1d8b3..40e098f2914e8c606d69c95a62234a53c12b9f10 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
+  WRAP_LEN = 80 # wrap messages and text attachments at this width
+
   class Attachment
     HookManager.register "mime-decode", <<EOS
-Executes when decoding a MIME attachment.
+Decodes a MIME attachment into text form. The text will be displayed
+directly in Sup. For attachments that you wish to use a separate program
+to view (e.g. images), you should use the mime-view hook instead.
+
 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
+Views a non-text MIME attachment. This hook allows you to run
+third-party programs for attachments that require such a thing (e.g.
+images). To instead display a text version of the attachment directly in
+Sup, use the mime-decode hook instead.
+
+Note that by default (at least on systems that have a run-mailcap command),
+Sup uses the default mailcap handler for the attachment's MIME type. If
+you want a particular behavior to be global, you may wish to change your
+mailcap instead.
+
+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. If false, calling
+  /usr/bin/run-mailcap will be tried.
+EOS
 #' stupid ruby-mode
 
     ## raw_content is the post-MIME-decode content. this is used for
@@ -54,7 +87,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 +96,22 @@ 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")
+          Iconv.easy_decode $encoding, encoded_content.charset || $encoding, @raw_content
         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")
+        @lines = lines.map {|l| l.chomp.wrap WRAP_LEN}.flatten
+        @quotable = true
+      end
     end
 
     def color; :none end
@@ -83,7 +120,7 @@ EOS
       if expandable?
         "Attachment: #{filename} (#{lines.length} lines)"
       else
-        "Attachment: #{filename} (#{content_type})"
+        "Attachment: #{filename} (#{content_type}; #{@raw_content.size.to_human_size})"
       end
     end
 
@@ -93,14 +130,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}'"
+      debug "running: #{cmd.inspect}"
+      BufferManager.shell_out(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
+      ret || view_default!(path)
     end
 
     def write_to_disk
-      file = Tempfile.new "redwood.attachment"
+      file = Tempfile.new(@filename || "sup-attachment")
       file.print @raw_content
       file.close
       file.path
@@ -113,7 +158,6 @@ EOS
   end
 
   class Text
-    WRAP_LEN = 80 # wrap at this width
 
     attr_reader :lines
     def initialize lines
@@ -176,7 +220,7 @@ EOS
     def inlineable?; false end
     def quotable?; false end
     def expandable?; true end
-    def initial_state; :open end
+    def initial_state; :closed end
     def viewable?; false end
 
     def patina_color; :generic_notice_patina_color end
@@ -196,8 +240,8 @@ EOS
 
     def patina_color
       case status
-      when :valid: :cryptosig_valid_color
-      when :invalid: :cryptosig_invalid_color
+      when :valid then :cryptosig_valid_color
+      when :invalid then :cryptosig_invalid_color
       else :cryptosig_unknown_color
       end
     end