class Redwood::Chunk::Attachment

Attributes

content_type[R]

#raw_content is the post-MIME-decode content. this is used for saving the attachment to disk.

filename[R]

#raw_content is the post-MIME-decode content. this is used for saving the attachment to disk.

lines[R]

#raw_content is the post-MIME-decode content. this is used for saving the attachment to disk.

raw_content[R]

#raw_content is the post-MIME-decode content. this is used for saving the attachment to disk.

Public Class Methods

new(content_type, filename, encoded_content, sibling_types) click to toggle source
# File lib/sup/message_chunks.rb, line 111
def initialize content_type, filename, encoded_content, sibling_types
  @content_type = content_type.downcase
  if Shellwords.escape(@content_type) != @content_type
    warn "content_type #{@content_type} is not safe, changed to application/octet-stream"
    @content_type = 'application/octet-stream'
  end

  @filename = filename
  @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

  text = case @content_type
  when /^text\/plain\b/
    @raw_content
  else
    HookManager.run "mime-decode", :content_type => @content_type,
                    :filename => lambda { write_to_disk },
                    :charset => encoded_content.charset,
                    :sibling_types => sibling_types
  end

  @lines = nil
  if text
    text = text.transcode(encoded_content.charset || $encoding, text.encoding)
    begin
      @lines = text.gsub("\r\n", "\n").gsub(/\t/, "        ").gsub(/\r/, "").split("\n")
    rescue Encoding::CompatibilityError
      @lines = text.fix_encoding!.gsub("\r\n", "\n").gsub(/\t/, "        ").gsub(/\r/, "").split("\n")
      debug "error while decoding message text, falling back to default encoding, expect errors in encoding: #{text.fix_encoding!}"
    end

    @quotable = true
  end
end

Public Instance Methods

color() click to toggle source
# File lib/sup/message_chunks.rb, line 152
def color; :text_color end
expandable?() click to toggle source
# File lib/sup/message_chunks.rb, line 166
def expandable?; !viewable? end
indexable?() click to toggle source
# File lib/sup/message_chunks.rb, line 167
def indexable?; expandable? end
initial_state() click to toggle source
# File lib/sup/message_chunks.rb, line 168
def initial_state; :open end
inlineable?() click to toggle source

an attachment is exapndable if we've managed to decode it into something we can display inline. otherwise, it's viewable.

# File lib/sup/message_chunks.rb, line 165
def inlineable?; false end
patina_color() click to toggle source
# File lib/sup/message_chunks.rb, line 153
def patina_color; :attachment_color end
patina_text() click to toggle source
# File lib/sup/message_chunks.rb, line 154
def patina_text
  if expandable?
    "Attachment: #{filename} (#{lines.length} lines)"
  else
    "Attachment: #{filename} (#{content_type}; #{@raw_content.size.to_human_size})"
  end
end
safe_filename() click to toggle source
# File lib/sup/message_chunks.rb, line 161
def safe_filename; Shellwords.escape(@filename).gsub("/", "_") end
to_s() click to toggle source

used when viewing the attachment as text

# File lib/sup/message_chunks.rb, line 216
def to_s
  @lines || @raw_content
end
view!() click to toggle source
# File lib/sup/message_chunks.rb, line 182
def view!
  write_to_disk do |path|
    ret = HookManager.run "mime-view", :content_type => @content_type,
                                       :filename => path
    ret || view_default!(path)
  end
end
view_default!(path) click to toggle source
# File lib/sup/message_chunks.rb, line 170
def view_default! path
  case RbConfig::CONFIG['arch']
    when /darwin/
      cmd = "open #{path}"
    else
      cmd = "/usr/bin/run-mailcap --action=view #{@content_type}:#{path}"
  end
  debug "running: #{cmd.inspect}"
  BufferManager.shell_out(cmd)
  $? == 0
end
viewable?() click to toggle source
# File lib/sup/message_chunks.rb, line 169
def viewable?; @lines.nil? end
write_to_disk() { |path| ... } click to toggle source
# File lib/sup/message_chunks.rb, line 190
def write_to_disk
  begin
    # Add the original extension to the generated tempfile name only if the
    # extension is "safe" (won't be interpreted by the shell).  Since
    # Tempfile.new always generates safe file names this should prevent
    # attacking the user with funny attachment file names.
    tempname = if (File.extname @filename) =~ /^\.[[:alnum:]]+$/ then
                 ["sup-attachment", File.extname(@filename)]
               else
                 "sup-attachment"
               end

    file = Tempfile.new(tempname)
    file.print @raw_content
    file.flush

    @@view_tempfiles.push file # make sure the tempfile is not garbage collected before sup stops

    yield file.path if block_given?
    return file.path
  ensure
    file.close
  end
end