]> git.cworth.org Git - sup/blob - lib/sup/message-chunks.rb
use BufferManager.shell_out to call run-mailcap instead of system
[sup] / lib / sup / message-chunks.rb
1 require 'tempfile'
2
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
9 ## at display time.
10 ##
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.
15 ##
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).
23 ##
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.
29 ##
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.
33
34 ## monkey-patch time: make temp files have the right extension
35 class Tempfile
36   def make_tmpname basename, n
37     sprintf '%d-%d-%s', $$, n, basename
38   end
39 end
40
41
42 module Redwood
43 module Chunk
44   WRAP_LEN = 80 # wrap messages and text attachments at this width
45
46   class Attachment
47     HookManager.register "mime-decode", <<EOS
48 Decodes a MIME attachment into text form. The text will be displayed
49 directly in Sup. For attachments that you wish to use a separate program
50 to view (e.g. images), you should use the mime-view hook instead.
51
52 Variables:
53    content_type: the content-type of the message
54        filename: the filename of the attachment as saved to disk
55   sibling_types: if this attachment is part of a multipart MIME attachment,
56                  an array of content-types for all attachments. Otherwise,
57                  the empty array.
58 Return value:
59   The decoded text of the attachment, or nil if not decoded.
60 EOS
61
62     HookManager.register "mime-view", <<EOS
63 Views a non-text MIME attachment. This hook allows you to run
64 third-party programs for attachments that require such a thing (e.g.
65 images). To instead display a text version of the attachment directly in
66 Sup, use the mime-decode hook instead.
67
68 Note that by default (at least on systems that have a run-mailcap command),
69 Sup uses the default mailcap handler for the attachment's MIME type. If
70 you want a particular behavior to be global, you may wish to change your
71 mailcap instead.
72
73 Variables:
74    content_type: the content-type of the attachment
75        filename: the filename of the attachment as saved to disk
76 Return value:
77   True if the viewing was successful, false otherwise. If false, calling
78   /usr/bin/run-mailcap will be tried.
79 EOS
80 #' stupid ruby-mode
81
82     ## raw_content is the post-MIME-decode content. this is used for
83     ## saving the attachment to disk.
84     attr_reader :content_type, :filename, :lines, :raw_content
85     bool_reader :quotable
86
87     def initialize content_type, filename, encoded_content, sibling_types
88       @content_type = content_type
89       @filename = filename
90       @quotable = false # changed to true if we can parse it through the
91                         # mime-decode hook, or if it's plain text
92       @raw_content =
93         if encoded_content.body
94           encoded_content.decode
95         else
96           "For some bizarre reason, RubyMail was unable to parse this attachment.\n"
97         end
98
99       text =
100         case @content_type
101         when /^text\/plain\b/
102           Iconv.easy_decode $encoding, encoded_content.charset || $encoding, @raw_content
103         else
104           HookManager.run "mime-decode", :content_type => content_type,
105                           :filename => lambda { write_to_disk },
106                           :sibling_types => sibling_types
107         end
108
109       @lines = nil
110       if text
111         @lines = text.gsub("\r\n", "\n").gsub(/\t/, "        ").gsub(/\r/, "").split("\n")
112         @lines = lines.map {|l| l.chomp.wrap WRAP_LEN}.flatten
113         @quotable = true
114       end
115     end
116
117     def color; :none end
118     def patina_color; :attachment_color end
119     def patina_text
120       if expandable?
121         "Attachment: #{filename} (#{lines.length} lines)"
122       else
123         "Attachment: #{filename} (#{content_type}; #{@raw_content.size.to_human_size})"
124       end
125     end
126
127     ## an attachment is exapndable if we've managed to decode it into
128     ## something we can display inline. otherwise, it's viewable.
129     def inlineable?; false end
130     def expandable?; !viewable? end
131     def initial_state; :open end
132     def viewable?; @lines.nil? end
133     def view_default! path
134       cmd = "/usr/bin/run-mailcap --action=view '#{@content_type}:#{path}'"
135       Redwood::log "running: #{cmd.inspect}"
136       BufferManager.shell_out(cmd)
137       $? == 0
138     end
139
140     def view!
141       path = write_to_disk
142       ret = HookManager.run "mime-view", :content_type => @content_type,
143                                          :filename => path
144       ret || view_default!(path)
145     end
146
147     def write_to_disk
148       file = Tempfile.new(@filename || "sup-attachment")
149       file.print @raw_content
150       file.close
151       file.path
152     end
153
154     ## used when viewing the attachment as text
155     def to_s
156       @lines || @raw_content
157     end
158   end
159
160   class Text
161
162     attr_reader :lines
163     def initialize lines
164       @lines = lines.map { |l| l.chomp.wrap WRAP_LEN }.flatten # wrap
165
166       ## trim off all empty lines except one
167       @lines.pop while @lines.length > 1 && @lines[-1] =~ /^\s*$/ && @lines[-2] =~ /^\s*$/
168     end
169
170     def inlineable?; true end
171     def quotable?; true end
172     def expandable?; false end
173     def viewable?; false end
174     def color; :none end
175   end
176
177   class Quote
178     attr_reader :lines
179     def initialize lines
180       @lines = lines
181     end
182     
183     def inlineable?; @lines.length == 1 end
184     def quotable?; true end
185     def expandable?; !inlineable? end
186     def viewable?; false end
187
188     def patina_color; :quote_patina_color end
189     def patina_text; "(#{lines.length} quoted lines)" end
190     def color; :quote_color end
191   end
192
193   class Signature
194     attr_reader :lines
195     def initialize lines
196       @lines = lines
197     end
198
199     def inlineable?; @lines.length == 1 end
200     def quotable?; false end
201     def expandable?; !inlineable? end
202     def viewable?; false end
203
204     def patina_color; :sig_patina_color end
205     def patina_text; "(#{lines.length}-line signature)" end
206     def color; :sig_color end
207   end
208
209   class EnclosedMessage
210     attr_reader :lines
211     def initialize from, body
212       @from = from
213       @lines = body.split "\n"
214     end
215
216     def from
217       @from ? @from.longname : "unknown sender"
218     end
219
220     def inlineable?; false end
221     def quotable?; false end
222     def expandable?; true end
223     def initial_state; :closed end
224     def viewable?; false end
225
226     def patina_color; :generic_notice_patina_color end
227     def patina_text; "Begin enclosed message from #{from} (#{@lines.length} lines)" end
228
229     def color; :quote_color end
230   end
231
232   class CryptoNotice
233     attr_reader :lines, :status, :patina_text
234
235     def initialize status, description, lines=[]
236       @status = status
237       @patina_text = description
238       @lines = lines
239     end
240
241     def patina_color
242       case status
243       when :valid then :cryptosig_valid_color
244       when :invalid then :cryptosig_invalid_color
245       else :cryptosig_unknown_color
246       end
247     end
248     def color; patina_color end
249
250     def inlineable?; false end
251     def quotable?; false end
252     def expandable?; !@lines.empty? end
253     def viewable?; false end
254   end
255 end
256 end