3 ## Here we define all the "chunks" that a message is parsed
4 ## into. Chunks are used by ThreadViewMode to render a message. Chunks
5 ## are used for both MIME stuff like attachments, for Sup's parsing of
6 ## the message body into text, quote, and signature regions, and for
7 ## notices like "this message was decrypted" or "this message contains
8 ## a valid signature"---basically, anything we want to differentiate
11 ## A chunk can be inlineable, expandable, or viewable. If it's
12 ## inlineable, #color and #lines are called and the output is treated
13 ## as part of the message text. This is how Text and one-line Quotes
14 ## and Signatures work.
16 ## If it's not inlineable but is expandable, #patina_color and
17 ## #patina_text are called to generate a "patina" (a one-line widget,
18 ## basically), and the user can press enter to toggle the display of
19 ## the chunk content, which is generated from #color and #lines as
20 ## above. This is how Quote, Signature, and most widgets
21 ## work. Exandable chunks can additionally define #initial_state to be
22 ## :open if they want to start expanded (default is to start collapsed).
24 ## If it's not expandable but is viewable, a patina is displayed using
25 ## #patina_color and #patina_text, but no toggling is allowed. Instead,
26 ## if #view! is defined, pressing enter on the widget calls view! and
27 ## (if that returns false) #to_s. Otherwise, enter does nothing. This
28 ## is how non-inlineable attachments work.
30 ## Independent of all that, a chunk can be quotable, in which case it's
31 ## included as quoted text during a reply. Text, Quotes, and mime-parsed
32 ## attachments are quotable; Signatures are not.
34 ## monkey-patch time: make temp files have the right extension
36 def make_tmpname basename, n
37 sprintf '%d-%d-%s', $$, n, basename
44 WRAP_LEN = 80 # wrap messages and text attachments at this width
47 HookManager.register "mime-decode", <<EOS
48 Executes when decoding a MIME attachment.
50 content_type: the content-type of the message
51 filename: the filename of the attachment as saved to disk
52 sibling_types: if this attachment is part of a multipart MIME attachment,
53 an array of content-types for all attachments. Otherwise,
56 The decoded text of the attachment, or nil if not decoded.
59 HookManager.register "mime-view", <<EOS
60 Executes when viewing a MIME attachment, i.e., launching a separate
63 content_type: the content-type of the attachment
64 filename: the filename of the attachment as saved to disk
66 True if the viewing was successful, false otherwise.
70 ## raw_content is the post-MIME-decode content. this is used for
71 ## saving the attachment to disk.
72 attr_reader :content_type, :filename, :lines, :raw_content
75 def initialize content_type, filename, encoded_content, sibling_types
76 @content_type = content_type
78 @quotable = false # changed to true if we can parse it through the
79 # mime-decode hook, or if it's plain text
81 if encoded_content.body
82 encoded_content.decode
84 "For some bizarre reason, RubyMail was unable to parse this attachment.\n"
90 Message.convert_from @raw_content, encoded_content.charset
92 HookManager.run "mime-decode", :content_type => content_type,
93 :filename => lambda { write_to_disk },
94 :sibling_types => sibling_types
99 @lines = text.gsub("\r\n", "\n").gsub(/\t/, " ").gsub(/\r/, "").split("\n")
100 @lines = lines.map {|l| l.chomp.wrap WRAP_LEN}.flatten
106 def patina_color; :attachment_color end
109 "Attachment: #{filename} (#{lines.length} lines)"
111 "Attachment: #{filename} (#{content_type})"
115 ## an attachment is exapndable if we've managed to decode it into
116 ## something we can display inline. otherwise, it's viewable.
117 def inlineable?; false end
118 def expandable?; !viewable? end
119 def initial_state; :open end
120 def viewable?; @lines.nil? end
121 def view_default! path
122 cmd = "/usr/bin/run-mailcap --action=view '#{@content_type}:#{path}' 2>/dev/null"
123 Redwood::log "running: #{cmd.inspect}"
130 ret = HookManager.run "mime-view", :content_type => @content_type,
132 ret || view_default!(path)
136 file = Tempfile.new(@filename || "sup-attachment")
137 file.print @raw_content
142 ## used when viewing the attachment as text
144 @lines || @raw_content
152 @lines = lines.map { |l| l.chomp.wrap WRAP_LEN }.flatten # wrap
154 ## trim off all empty lines except one
155 @lines.pop while @lines.length > 1 && @lines[-1] =~ /^\s*$/ && @lines[-2] =~ /^\s*$/
158 def inlineable?; true end
159 def quotable?; true end
160 def expandable?; false end
161 def viewable?; false end
171 def inlineable?; @lines.length == 1 end
172 def quotable?; true end
173 def expandable?; !inlineable? end
174 def viewable?; false end
176 def patina_color; :quote_patina_color end
177 def patina_text; "(#{lines.length} quoted lines)" end
178 def color; :quote_color end
187 def inlineable?; @lines.length == 1 end
188 def quotable?; false end
189 def expandable?; !inlineable? end
190 def viewable?; false end
192 def patina_color; :sig_patina_color end
193 def patina_text; "(#{lines.length}-line signature)" end
194 def color; :sig_color end
197 class EnclosedMessage
199 def initialize from, body
201 @lines = body.split "\n"
205 @from ? @from.longname : "unknown sender"
208 def inlineable?; false end
209 def quotable?; false end
210 def expandable?; true end
211 def initial_state; :closed end
212 def viewable?; false end
214 def patina_color; :generic_notice_patina_color end
215 def patina_text; "Begin enclosed message from #{from} (#{@lines.length} lines)" end
217 def color; :quote_color end
221 attr_reader :lines, :status, :patina_text
223 def initialize status, description, lines=[]
225 @patina_text = description
231 when :valid: :cryptosig_valid_color
232 when :invalid: :cryptosig_invalid_color
233 else :cryptosig_unknown_color
236 def color; patina_color end
238 def inlineable?; false end
239 def quotable?; false end
240 def expandable?; !@lines.empty? end
241 def viewable?; false end