X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Fmessage-chunks.rb;h=40e098f2914e8c606d69c95a62234a53c12b9f10;hb=38b3d2167248fd42b7057edf3036215937fe2ed4;hp=9168aa7c51d703e39abfaf06d72572e5dc97e813;hpb=d17cff51b71fddeece6702c25e817bb21eafe8cb;p=sup diff --git a/lib/sup/message-chunks.rb b/lib/sup/message-chunks.rb index 9168aa7..40e098f 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,47 +22,96 @@ ## :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 + WRAP_LEN = 80 # wrap messages and text attachments at this width + class Attachment HookManager.register "mime-decode", < 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") + @lines = lines.map {|l| l.chomp.wrap WRAP_LEN}.flatten + @quotable = true + end end def color; :none end @@ -69,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 @@ -79,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" - $? == 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 @@ -99,17 +158,17 @@ EOS end class Text - WRAP_LEN = 80 # wrap at this width attr_reader :lines def initialize lines @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 @@ -122,6 +181,7 @@ EOS end def inlineable?; @lines.length == 1 end + def quotable?; true end def expandable?; !inlineable? end def viewable?; false end @@ -137,6 +197,7 @@ EOS end def inlineable?; @lines.length == 1 end + def quotable?; false end def expandable?; !inlineable? end def viewable?; false end @@ -146,19 +207,24 @@ EOS end class EnclosedMessage - attr_reader :from, :lines + attr_reader :lines def initialize from, body @from = from @lines = body.split "\n" end + def from + @from ? @from.longname : "unknown sender" + 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 - def patina_text; "Begin enclosed message from #{@from.longname} (#{@lines.length} lines)" end + def patina_text; "Begin enclosed message from #{from} (#{@lines.length} lines)" end def color; :quote_color end end @@ -174,14 +240,15 @@ 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 def color; patina_color end def inlineable?; false end + def quotable?; false end def expandable?; !@lines.empty? end def viewable?; false end end