lib/sup/message-chunks.rb in sup-0.3 vs lib/sup/message-chunks.rb in sup-0.4

- old
+ new

@@ -1,5 +1,7 @@ +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 ## the message body into text, quote, and signature regions, and for ## notices like "this message was decrypted" or "this message contains @@ -27,56 +29,76 @@ ## ## Independent of all that, a chunk can be quotable, in which case it's ## 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 HookManager.register "mime-decode", <<EOS 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 ## saving the attachment to disk. attr_reader :content_type, :filename, :lines, :raw_content bool_reader :quotable 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 else "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 def patina_color; :attachment_color end def patina_text @@ -91,17 +113,23 @@ ## something we can display inline. otherwise, it's viewable. def inlineable?; false end def expandable?; !viewable? end def initial_state; :open end def viewable?; @lines.nil? end - def view! - path = write_to_disk + def view_default! path system "/usr/bin/run-mailcap --action=view #{@content_type}:#{path} > /dev/null 2> /dev/null" $? == 0 end + def view! + path = write_to_disk + 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 end