]> git.cworth.org Git - sup/blob - lib/sup/message-chunks.rb
make text/plain attachments part of the reply (patch from nicolas pouillard)
[sup] / lib / sup / message-chunks.rb
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
7 ## at display time.
8 ##
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.
13 ##
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
19 ## work. Exandable chunks can additionally define #initial_state to be
20 ## :open if they want to start expanded (default is to start collapsed).
21 ##
22 ## If it's not expandable but is viewable, a patina is displayed using
23 ## #patina_color and #patina_text, but no toggling is allowed. Instead,
24 ## if #view! is defined, pressing enter on the widget calls view! and
25 ## (if that returns false) #to_s. Otherwise, enter does nothing. This
26 ##  is how non-inlineable attachments work.
27 ##
28 ## Independent of all that, a chunk can be quotable, in which case it's
29 ## included as quoted text during a reply. Text, Quotes, and mime-parsed
30 ## attachments are quotable; Signatures are not.
31
32 module Redwood
33 module Chunk
34   class Attachment
35     HookManager.register "mime-decode", <<EOS
36 Executes when decoding a MIME attachment.
37 Variables:
38    content_type: the content-type of the message
39        filename: the filename of the attachment as saved to disk (generated
40                  on the fly, so don't call more than once)
41   sibling_types: if this attachment is part of a multipart MIME attachment,
42                  an array of content-types for all attachments. Otherwise,
43                  the empty array.
44 Return value:
45   The decoded text of the attachment, or nil if not decoded.
46 EOS
47 #' stupid ruby-mode
48
49     ## raw_content is the post-MIME-decode content. this is used for
50     ## saving the attachment to disk.
51     attr_reader :content_type, :filename, :lines, :raw_content
52     bool_reader :quotable
53
54     def initialize content_type, filename, encoded_content, sibling_types
55       @content_type = content_type
56       @filename = filename
57       @quotable = false # changed to true if we can parse it through the
58                         # mime-decode hook, or if it's plain text
59       @raw_content =
60         if encoded_content.body
61           encoded_content.decode
62         else
63           "For some bizarre reason, RubyMail was unable to parse this attachment.\n"
64         end
65
66       @lines =
67         case @content_type
68         when /^text\/plain\b/
69           @quotable = true
70           Message.convert_from(@raw_content, encoded_content.charset).split("\n")
71         else
72           text = HookManager.run "mime-decode", :content_type => content_type,
73                                  :filename => lambda { write_to_disk },
74                                  :sibling_types => sibling_types
75           if text
76             @quotable = true
77             text.split("\n")
78           end
79         end
80     end
81
82     def color; :none end
83     def patina_color; :attachment_color end
84     def patina_text
85       if expandable?
86         "Attachment: #{filename} (#{lines.length} lines)"
87       else
88         "Attachment: #{filename} (#{content_type})"
89       end
90     end
91
92     ## an attachment is exapndable if we've managed to decode it into
93     ## something we can display inline. otherwise, it's viewable.
94     def inlineable?; false end
95     def expandable?; !viewable? end
96     def initial_state; :open end
97     def viewable?; @lines.nil? end
98     def view!
99       path = write_to_disk
100       system "/usr/bin/run-mailcap --action=view #{@content_type}:#{path} > /dev/null 2> /dev/null"
101       $? == 0
102     end
103
104     def write_to_disk
105       file = Tempfile.new "redwood.attachment"
106       file.print @raw_content
107       file.close
108       file.path
109     end
110
111     ## used when viewing the attachment as text
112     def to_s
113       @lines || @raw_content
114     end
115   end
116
117   class Text
118     WRAP_LEN = 80 # wrap at this width
119
120     attr_reader :lines
121     def initialize lines
122       @lines = lines.map { |l| l.chomp.wrap WRAP_LEN }.flatten # wrap
123
124       ## trim off all empty lines except one
125       @lines.pop while @lines.length > 1 && @lines[-1] =~ /^\s*$/ && @lines[-2] =~ /^\s*$/
126     end
127
128     def inlineable?; true end
129     def quotable?; true end
130     def expandable?; false end
131     def viewable?; false end
132     def color; :none end
133   end
134
135   class Quote
136     attr_reader :lines
137     def initialize lines
138       @lines = lines
139     end
140     
141     def inlineable?; @lines.length == 1 end
142     def quotable?; true end
143     def expandable?; !inlineable? end
144     def viewable?; false end
145
146     def patina_color; :quote_patina_color end
147     def patina_text; "(#{lines.length} quoted lines)" end
148     def color; :quote_color end
149   end
150
151   class Signature
152     attr_reader :lines
153     def initialize lines
154       @lines = lines
155     end
156
157     def inlineable?; @lines.length == 1 end
158     def quotable?; false end
159     def expandable?; !inlineable? end
160     def viewable?; false end
161
162     def patina_color; :sig_patina_color end
163     def patina_text; "(#{lines.length}-line signature)" end
164     def color; :sig_color end
165   end
166
167   class EnclosedMessage
168     attr_reader :lines
169     def initialize from, body
170       @from = from
171       @lines = body.split "\n"
172     end
173
174     def from
175       @from ? @from.longname : "unknown sender"
176     end
177
178     def inlineable?; false end
179     def quotable?; false end
180     def expandable?; true end
181     def initial_state; :open end
182     def viewable?; false end
183
184     def patina_color; :generic_notice_patina_color end
185     def patina_text; "Begin enclosed message from #{from} (#{@lines.length} lines)" end
186
187     def color; :quote_color end
188   end
189
190   class CryptoNotice
191     attr_reader :lines, :status, :patina_text
192
193     def initialize status, description, lines=[]
194       @status = status
195       @patina_text = description
196       @lines = lines
197     end
198
199     def patina_color
200       case status
201       when :valid: :cryptosig_valid_color
202       when :invalid: :cryptosig_invalid_color
203       else :cryptosig_unknown_color
204       end
205     end
206     def color; patina_color end
207
208     def inlineable?; false end
209     def quotable?; false end
210     def expandable?; !@lines.empty? end
211     def viewable?; false end
212   end
213 end
214 end