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