6 class MessageFormatError < StandardError; end
8 ## a Message is what's threaded.
10 ## it is also where the parsing for quotes and signatures is done, but
11 ## that should be moved out to a separate class at some point (because
12 ## i would like, for example, to be able to add in a ruby-talk
13 ## specific module that would detect and link to /ruby-talk:\d+/
14 ## sequences in the text of an email. (how sweet would that be?)
16 ## TODO: integrate with user's addressbook to render names
20 RE_PATTERN = /^((re|re[\[\(]\d[\]\)]):\s*)+/i
22 ## some utility methods
24 def normalize_subj s; s.gsub(RE_PATTERN, ""); end
25 def subj_is_reply? s; s =~ RE_PATTERN; end
26 def reify_subj s; subj_is_reply?(s) ? s : "Re: " + s; end
30 attr_reader :content_type, :desc
31 def initialize content_type, desc, part
32 @content_type = content_type
40 @file = Tempfile.new "redwood.attachment"
41 @file.print @part.decode
45 ## TODO: handle unknown mime-types
46 system "/usr/bin/run-mailcap --action=view #{@content_type}:#{@file.path}"
54 @lines = lines.map { |l| l.wrap 80 }.flatten
72 QUOTE_PATTERN = /^\s{0,4}[>|\}]/
73 BLOCK_QUOTE_PATTERN = /^-----\s*Original Message\s*----+$/
74 QUOTE_START_PATTERN = /(^\s*Excerpts from)|(^\s*In message )|(^\s*In article )|(^\s*Quoting )|((wrote|writes|said|says)\s*:\s*$)/
75 SIG_PATTERN = /(^-- ?$)|(^\s*----------+\s*$)|(^\s*_________+\s*$)/
76 SIG_DISTANCE = 15 # lines from the end
77 DEFAULT_SUBJECT = "(missing subject)"
78 DEFAULT_SENDER = "(missing sender)"
80 attr_reader :id, :date, :from, :subj, :refs, :replytos, :to, :source,
81 :cc, :bcc, :labels, :list_address, :recipient_email, :replyto,
82 :source_info, :mbox_status
86 def initialize source, source_info, labels, snippet=nil
88 @source_info = source_info
93 header = @source.load_header @source_info
94 header.each { |k, v| header[k.downcase] = v }
96 %w(message-id date).each do |f|
97 raise MessageFormatError, "no #{f} field in header #{header.inspect} (source #@source offset #@source_info)" unless header.include? f
98 raise MessageFormatError, "nil #{f} field in header #{header.inspect} (source #@source offset #@source_info)" unless header[f]
102 @date = Time.parse header["date"]
103 rescue ArgumentError => e
104 raise MessageFormatError, "unparsable date #{header['date']}: #{e.message}"
107 if(@subj = header["subject"])
108 @subj = @subj.gsub(/\s+/, " ").gsub(/\s+$/, "")
110 @subj = DEFAULT_SUBJECT
112 @from = Person.for header["from"]
113 @to = Person.for_several header["to"]
114 @cc = Person.for_several header["cc"]
115 @bcc = Person.for_several header["bcc"]
116 @id = header["message-id"]
117 @refs = (header["references"] || "").scan(/<(.*?)>/).flatten
118 @replytos = (header["in-reply-to"] || "").scan(/<(.*?)>/).flatten
119 @replyto = Person.for header["reply-to"]
121 if header["list-post"]
122 @list_address = Person.for header["list-post"].gsub(/^<mailto:|>$/, "")
127 @recipient_email = header["delivered-to"]
128 @mbox_status = header["status"]
132 to_chunks unless @snippet
136 def is_list_message?; !@list_address.nil?; end
137 def is_draft?; DraftLoader === @source; end
139 raise "not a draft" unless is_draft?
140 @source.fn_for_offset @source_info
144 index.update_message self if @dirty
148 def has_label? t; @labels.member? t; end
150 return if @labels.member? t
155 return unless @labels.member? t
170 m = @source.load_message @source_info
175 @source.load_header_text @source_info
180 from && from.longname,
181 to.map { |p| p.longname },
182 cc.map { |p| p.longname },
183 bcc.map { |p| p.longname },
184 to_chunks.select { |c| c.is_a? Text }.map { |c| c.lines },
186 ].flatten.compact.join " "
190 to_chunks.find_all { |c| c.is_a?(Text) || c.is_a?(Quote) }.map { |c| c.lines }.flatten
193 def basic_header_lines
194 ["From: #{@from.full_address}"] +
195 (@to.empty? ? [] : ["To: " + @to.map { |p| p.full_address }.join(", ")]) +
196 (@cc.empty? ? [] : ["Cc: " + @cc.map { |p| p.full_address }.join(", ")]) +
197 (@bcc.empty? ? [] : ["Bcc: " + @bcc.map { |p| p.full_address }.join(", ")]) +
198 ["Date: #{@date.rfc822}",
204 ## everything RubyMail-specific goes here.
205 def message_to_chunks m
207 case m.header.content_type
208 when "text/plain", nil
209 raise MessageFormatError, "no message body before decode" unless
211 body = m.decode or raise MessageFormatError, "no message body"
212 text_to_chunks body.gsub(/\t/, " ").gsub(/\r/, "").split("\n")
213 when "multipart/alternative", "multipart/mixed"
216 disp = m.header["Content-Disposition"] || ""
217 Attachment.new m.header.content_type, disp.gsub(/[\s\n]+/, " "), m
220 m.each_part { |p| ret << message_to_chunks(p) } if m.multipart?
224 ## parse the lines of text into chunk objects. the heuristics here
225 ## need tweaking in some nice manner. TODO: move these heuristics
226 ## into the classes themselves.
228 def text_to_chunks lines
229 state = :text # one of :text, :quote, or :sig
233 lines.each_with_index do |line, i|
234 nextline = lines[(i + 1) ... lines.length].find { |l| l !~ /^\s*$/ } # skip blank lines
238 if line =~ QUOTE_PATTERN || (line =~ QUOTE_START_PATTERN && (nextline =~ QUOTE_PATTERN || nextline =~ QUOTE_START_PATTERN))
240 elsif line =~ SIG_PATTERN && (lines.length - i) < SIG_DISTANCE
242 elsif line =~ BLOCK_QUOTE_PATTERN
243 newstate = :block_quote
246 chunks << Text.new(chunk_lines) unless chunk_lines.empty?
254 if line =~ QUOTE_PATTERN || line =~ QUOTE_START_PATTERN || line =~ /^\s*$/
256 elsif line =~ SIG_PATTERN && (lines.length - i) < SIG_DISTANCE
262 if chunk_lines.empty?
264 elsif chunk_lines.size == 1
265 chunks << Text.new(chunk_lines) # forget about one-line quotes
267 chunks << Quote.new(chunk_lines)
278 if state == :text && (@snippet.nil? || @snippet.length < SNIPPET_LEN) &&
279 line !~ /[=\*#_-]{3,}/ && line !~ /^\s*$/
280 @snippet = (@snippet ? @snippet + " " : "") + line.gsub(/^\s+/, "").gsub(/[\r\n]/, "").gsub(/\s+/, " ")
281 @snippet = @snippet[0 ... SNIPPET_LEN]
283 # if @snippet.nil? && state == :text && (line.length > 40 ||
284 # line =~ /\S+.*[^,!:]\s*$/)
285 # @snippet = line.gsub(/^\s+/, "").gsub(/[\r\n]/, "")[0 .. 80]
291 when :quote, :block_quote
292 chunks << Quote.new(chunk_lines) unless chunk_lines.empty?
294 chunks << Text.new(chunk_lines) unless chunk_lines.empty?
296 chunks << Signature.new(chunk_lines) unless chunk_lines.empty?