Class: Redwood::Chunk::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/sup/message_chunks.rb

Constant Summary collapse

@@view_tempfiles =

store tempfile objects as class variables so that they are not removed when the viewing process returns. they should be garbage collected when the class variable is removed.

[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type, filename, encoded_content, sibling_types) ⇒ Attachment

Returns a new instance of Attachment.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/sup/message_chunks.rb', line 112

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/
    if /^UTF-7$/i =~ encoded_content.charset
      @raw_content.decode_utf7
    else
      begin
        charset = Encoding.find(encoded_content.charset || 'US-ASCII')
      rescue ArgumentError
        charset = 'US-ASCII'
      end
      @raw_content.force_encoding(charset)
    end
  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.encode($encoding, :invalid => :replace, :undef => :replace)
    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

Instance Attribute Details

#content_typeObject (readonly)

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



104
105
106
# File 'lib/sup/message_chunks.rb', line 104

def content_type
  @content_type
end

#filenameObject (readonly)

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



104
105
106
# File 'lib/sup/message_chunks.rb', line 104

def filename
  @filename
end

#linesObject (readonly)

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



104
105
106
# File 'lib/sup/message_chunks.rb', line 104

def lines
  @lines
end

#raw_contentObject (readonly)

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



104
105
106
# File 'lib/sup/message_chunks.rb', line 104

def raw_content
  @raw_content
end

Instance Method Details

#colorObject



162
# File 'lib/sup/message_chunks.rb', line 162

def color; :text_color end

#expandable?Boolean

Returns:

  • (Boolean)


177
# File 'lib/sup/message_chunks.rb', line 177

def expandable?; !viewable? end

#filesafe_filenameObject



172
# File 'lib/sup/message_chunks.rb', line 172

def filesafe_filename; @filename.gsub("/", "_") end

#indexable?Boolean

Returns:

  • (Boolean)


178
# File 'lib/sup/message_chunks.rb', line 178

def indexable?; expandable? end

#initial_stateObject



179
# File 'lib/sup/message_chunks.rb', line 179

def initial_state; :open end

#inlineable?Boolean

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

Returns:

  • (Boolean)


176
# File 'lib/sup/message_chunks.rb', line 176

def inlineable?; false end

#patina_colorObject



163
# File 'lib/sup/message_chunks.rb', line 163

def patina_color; :attachment_color end

#patina_textObject



164
165
166
167
168
169
170
# File 'lib/sup/message_chunks.rb', line 164

def patina_text
  if expandable?
    "Attachment: #{filename} (#{lines.length} lines)"
  else
    "Attachment: #{filename} (#{content_type}; #{@raw_content.size.to_human_size})"
  end
end

#safe_filenameObject



171
# File 'lib/sup/message_chunks.rb', line 171

def safe_filename; Shellwords.escape(@filename).gsub("/", "_") end

#to_sObject

used when viewing the attachment as text



227
228
229
# File 'lib/sup/message_chunks.rb', line 227

def to_s
  @lines || @raw_content
end

#view!Object



193
194
195
196
197
198
199
# File 'lib/sup/message_chunks.rb', line 193

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) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/sup/message_chunks.rb', line 181

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?Boolean

Returns:

  • (Boolean)


180
# File 'lib/sup/message_chunks.rb', line 180

def viewable?; @lines.nil? end

#write_to_diskObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/sup/message_chunks.rb', line 201

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