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 not inlineable but is expandable, #patina_color and
15 ## #patina_text are called to generate a "patina" (a one-line widget,
16 ## basically), and the user can press enter to toggle the display of
17 ## the chunk content, which is generated from #color and #lines as
18 ## above. This is how Quote, Signature, and most widgets work.
20 ## If it's not expandable but is viewable, a patina is displayed using
21 ## #patina_color and #patina_text, but no toggling is allowed. Instead,
22 ## if #view! is defined, pressing enter on the widget calls view! and
23 ## (if that returns false) #to_s. Otherwise, enter does nothing. This
24 ## is how 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
81 system "/usr/bin/run-mailcap --action=view #{@content_type}:#{path} >& /dev/null"
86 file = Tempfile.new "redwood.attachment"
87 file.print @raw_content
92 ## used when viewing the attachment as text
94 @lines || @raw_content
99 WRAP_LEN = 80 # wrap at this width
103 @lines = lines.map { |l| l.chomp.wrap WRAP_LEN }.flatten # wrap
105 ## trim off all empty lines except one
106 lines.pop while lines.last =~ /^\s*$/
109 def inlineable?; true end
110 def expandable?; false end
111 def viewable?; false end
121 def inlineable?; @lines.length == 1 end
122 def expandable?; !inlineable? end
123 def viewable?; false end
125 def patina_color; :quote_patina_color end
126 def patina_text; "(#{lines.length} quoted lines)" end
127 def color; :quote_color end
136 def inlineable?; @lines.length == 1 end
137 def expandable?; !inlineable? end
138 def viewable?; false end
140 def patina_color; :sig_patina_color end
141 def patina_text; "(#{lines.length}-line signature)" end
142 def color; :sig_color end
145 class EnclosedMessageNotice
146 attr_reader :from, :lines
147 def initialize from, body
149 @lines = body.split "\n"
152 def inlineable?; false end
153 def expandable?; true end
154 def viewable?; false end
156 def patina_color; :generic_notice_patina_color end
157 def patina_text; "Begin enclosed message from #{@from.longname} (#{@lines.length} lines)" end
159 def color; :quote_color end
163 attr_reader :lines, :status, :patina_text
165 def initialize status, description, lines=[]
167 @patina_text = description
173 when :valid: :cryptosig_valid_color
174 when :invalid: :cryptosig_invalid_color
175 else :cryptosig_unknown_color
178 def color; patina_color end
180 def inlineable?; false end
181 def expandable?; !@lines.empty? end
182 def viewable?; false end