1 ## Here we define all the "chunks" that a message is parsed
2 ## into. Chunks are used by ThreadViewMode to render a message. Chunks
3 ## are used for both MIME stuff like attachments, for Sup's parsing of
4 ## the message body into text, quote, and signature regions, and for
5 ## notices like "this message was decrypted" or "this message contains
6 ## a valid signature"---basically, anything we want to differentiate
9 ## A chunk can be inlineable, expandable, or viewable. If it's
10 ## inlineable, #color and #lines are called and the output is treated
11 ## as part of the message text. This is how Text and one-line Quotes
12 ## and Signatures work.
14 ## If it's expandable, #patina_color and #patina_text are called to
15 ## generate a "patina" (a one-line widget, basically), and the user
16 ## can press enter to toggle the display of the chunk content, which
17 ## is generated from #color and #lines). This is how Quote, Signature,
18 ## and most widgets work.
20 ## If it's viewable, a patina is displayed using #patina_color and
21 ## #patina_text, but no toggling is allowed. Instead, if #view! is
22 ## defined, pressing enter on the widget calls view! and (if that
23 ## returns false) #to_s. Otherwise enter does nothing. This is how
24 ## non-inlineable attachments work.
29 HookManager.register "mime-decode", <<EOS
30 Executes when decoding a MIME attachment.
32 content_type: the content-type of the message
33 filename: the filename of the attachment as saved to disk (generated
34 on the fly, so don't call more than once)
35 sibling_types: if this attachment is part of a multipart MIME attachment,
36 an array of content-types for all attachments. Otherwise,
39 The decoded text of the attachment, or nil if not decoded.
43 ## raw_content is the post-MIME-decode content. this is used for
44 ## saving the attachment to disk.
45 attr_reader :content_type, :filename, :lines, :raw_content
47 def initialize content_type, filename, encoded_content, sibling_types
48 @content_type = content_type
50 @raw_content = encoded_content.decode
55 Message.convert_from(@raw_content, encoded_content.charset).split("\n")
57 text = HookManager.run "mime-decode", :content_type => content_type,
58 :filename => lambda { write_to_disk },
59 :sibling_types => sibling_types
60 text.split("\n") if text
65 def patina_color; :attachment_color end
68 "Attachment: #{filename} (#{lines.length} lines)"
70 "Attachment: #{filename} (#{content_type})"
74 ## an attachment is exapndable if we've managed to decode it into
75 ## something we can display inline. otherwise, it's viewable.
76 def inlineable?; false end
77 def expandable?; !viewable? end
78 def viewable?; @lines.nil? end
80 file = Tempfile.new "redwood.attachment"
81 file.print @raw_content
83 system "/usr/bin/run-mailcap --action=view #{@content_type}:#{file.path} >& /dev/null"
87 ## used when viewing the attachment as text
89 @lines || @raw_content
94 WRAP_LEN = 80 # wrap at this width
98 @lines = lines.map { |l| l.chomp.wrap WRAP_LEN }.flatten # wrap
100 ## trim off all empty lines except one
101 lines.pop while lines.last =~ /^\s*$/
105 def inlineable?; true end
114 def inlineable?; @lines.length == 1 end
115 def expandable?; !inlineable? end
116 def patina_color; :quote_patina_color end
117 def patina_text; "(#{lines.length} quoted lines)" end
118 def color; :quote_color end
127 def inlineable?; @lines.length == 1 end
128 def expandable?; !inlineable? end
129 def patina_color; :sig_patina_color end
130 def patina_text; "(#{lines.length}-line signature)" end
131 def color; :sig_color end
134 class EnclosedMessageNotice
141 "Begin enclosed message from #{@from.longname}"
146 attr_reader :lines, :status, :patina_text
148 def initialize status, description, lines=[]
150 @patina_text = description
156 when :valid: :cryptosig_valid_color
157 when :invalid: :cryptosig_invalid_color
158 else :cryptosig_unknown_color
161 def color; patina_color end
163 def inlineable?; false end
164 def expandable?; !@lines.empty? end
165 def viewable?; false end