X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Fmessage-chunks.rb;h=cc895f3e480e591d077a18a83a84b85ca7d0cf5a;hb=6831c5966aa0a6021978bffc071734d03cabf2b1;hp=477ba6412b478391c64f73f4689ecae7ad44526a;hpb=1f5cf842c54333b78a9d22ce1da1d5ef947128c5;p=sup diff --git a/lib/sup/message-chunks.rb b/lib/sup/message-chunks.rb index 477ba64..cc895f3 100644 --- a/lib/sup/message-chunks.rb +++ b/lib/sup/message-chunks.rb @@ -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 @@ -20,10 +22,22 @@ ## :open if they want to start expanded (default is to start collapsed). ## ## If it's not expandable but is viewable, a patina is displayed using -###patina_color and #patina_text, but no toggling is allowed. Instead, -##if #view! is defined, pressing enter on the widget calls view! and -##(if that returns false) #to_s. Otherwise, enter does nothing. This -##is how non-inlineable attachments work. +## #patina_color and #patina_text, but no toggling is allowed. Instead, +## if #view! is defined, pressing enter on the widget calls view! and +## (if that returns false) #to_s. Otherwise, enter does nothing. This +## is how non-inlineable attachments work. +## +## 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 @@ -32,23 +46,35 @@ 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", < content_type, - :filename => lambda { write_to_disk }, - :sibling_types => sibling_types - text.split("\n") if text + 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 @@ -84,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}' 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" - $? == 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 @@ -111,10 +150,11 @@ EOS @lines = lines.map { |l| l.chomp.wrap WRAP_LEN }.flatten # wrap ## trim off all empty lines except one - lines.pop while lines.last =~ /^\s*$/ + @lines.pop while @lines.length > 1 && @lines[-1] =~ /^\s*$/ && @lines[-2] =~ /^\s*$/ end def inlineable?; true end + def quotable?; true end def expandable?; false end def viewable?; false end def color; :none end @@ -127,6 +167,7 @@ EOS end def inlineable?; @lines.length == 1 end + def quotable?; true end def expandable?; !inlineable? end def viewable?; false end @@ -142,6 +183,7 @@ EOS end def inlineable?; @lines.length == 1 end + def quotable?; false end def expandable?; !inlineable? end def viewable?; false end @@ -162,8 +204,9 @@ EOS end 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 @@ -191,6 +234,7 @@ EOS def color; patina_color end def inlineable?; false end + def quotable?; false end def expandable?; !@lines.empty? end def viewable?; false end end