lib/sup/message.rb (26733B) - raw
1 # encoding: UTF-8
2
3 require 'time'
4 require 'string-scrub' if /^2\.0\./ =~ RUBY_VERSION
5
6 module Redwood
7
8 ## a Message is what's threaded.
9 ##
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?)
15
16 class Message
17 SNIPPET_LEN = 80
18 RE_PATTERN = /^((re|re[\[\(]\d[\]\)]):\s*)+/i
19
20 ## some utility methods
21 class << self
22 def normalize_subj s; s.gsub(RE_PATTERN, ""); end
23 def subj_is_reply? s; s =~ RE_PATTERN; end
24 def reify_subj s; subj_is_reply?(s) ? s : "Re: " + s; end
25 end
26
27 QUOTE_PATTERN = /^\s{0,4}[>|\}]/
28 BLOCK_QUOTE_PATTERN = /^-----\s*Original Message\s*----+$/
29 SIG_PATTERN = /(^(- )*-- ?$)|(^\s*----------+\s*$)|(^\s*_________+\s*$)|(^\s*--~--~-)|(^\s*--\+\+\*\*==)/
30
31 GPG_SIGNED_START = "-----BEGIN PGP SIGNED MESSAGE-----"
32 GPG_SIGNED_END = "-----END PGP SIGNED MESSAGE-----"
33 GPG_START = "-----BEGIN PGP MESSAGE-----"
34 GPG_END = "-----END PGP MESSAGE-----"
35 GPG_SIG_START = "-----BEGIN PGP SIGNATURE-----"
36 GPG_SIG_END = "-----END PGP SIGNATURE-----"
37
38 MAX_SIG_DISTANCE = 15 # lines from the end
39 DEFAULT_SUBJECT = ""
40 DEFAULT_SENDER = "(missing sender)"
41 MAX_HEADER_VALUE_SIZE = 4096
42
43 attr_reader :id, :date, :from, :subj, :refs, :replytos, :to,
44 :cc, :bcc, :labels, :attachments, :list_address, :recipient_email, :replyto,
45 :list_subscribe, :list_unsubscribe
46
47 bool_reader :dirty, :source_marked_read, :snippet_contains_encrypted_content
48
49 attr_accessor :locations
50
51 def initialize opts
52 @locations = opts[:locations] or raise ArgumentError, "locations can't be nil"
53 @snippet = opts[:snippet]
54 @snippet_contains_encrypted_content = false
55 @have_snippet = !(opts[:snippet].nil? || opts[:snippet].empty?)
56 @labels = Set.new(opts[:labels] || [])
57 @dirty = false
58 @encrypted = false
59 @chunks = nil
60 @attachments = []
61
62 ## we need to initialize this. see comments in parse_header as to
63 ## why.
64 @refs = []
65 end
66
67 def decode_header_field v
68 return unless v
69 return v unless v.is_a? String
70 return unless v.size < MAX_HEADER_VALUE_SIZE # avoid regex blowup on spam
71 ## Unfold per RFC 5322 2.2.3: remove each CRLF immediately followed
72 ## by WSP, leaving the WSP in place.
73 v = v.gsub(/\r?\n(?=[ \t])/, "")
74 ## Header values should be either 7-bit with RFC2047-encoded words
75 ## or UTF-8 as per RFC6532. Replace any invalid high bytes with U+FFFD.
76 Rfc2047.decode_to $encoding, v.force_encoding(Encoding::UTF_8).scrub
77 end
78
79 def parse_header encoded_header
80 header = SavingHash.new { |k| decode_header_field encoded_header[k] }
81
82 @id = ''
83 if header["message-id"]
84 mid = header["message-id"] =~ /<(.+?)>/ ? $1 : header["message-id"]
85 @id = sanitize_message_id mid
86 end
87 if (not @id.include? '@') || @id.length < 6
88 @id = "sup-faked-" + Digest::MD5.hexdigest(raw_header)
89 #from = header["from"]
90 #debug "faking non-existent message-id for message from #{from}: #{id}"
91 end
92
93 @from = Person.from_address(if header["from"]
94 header["from"]
95 else
96 name = "Sup Auto-generated Fake Sender <sup@fake.sender.example.com>"
97 #debug "faking non-existent sender for message #@id: #{name}"
98 name
99 end)
100
101 @date = case(date = header["date"])
102 when Time
103 date
104 when String
105 Time.rfc2822 date rescue nil
106 end
107 @date = location.fallback_date if @date.nil?
108 @date = Time.utc 1970, 1, 1 if @date.nil?
109
110 subj = header["subject"]
111 subj = subj ? subj.fix_encoding! : nil
112 @subj = subj ? subj.gsub(/\s+/, " ").gsub(/\s+$/, "") : DEFAULT_SUBJECT
113 @to = Person.from_address_list header["to"]
114 @cc = Person.from_address_list header["cc"]
115 @bcc = Person.from_address_list header["bcc"]
116
117 ## before loading our full header from the source, we can actually
118 ## have some extra refs set by the UI. (this happens when the user
119 ## joins threads manually). so we will merge the current refs values
120 ## in here.
121 refs = (header["references"] || "").scan(/<(.+?)>/).map { |x| sanitize_message_id x.first }
122 @refs = (@refs + refs).uniq
123 @replytos = (header["in-reply-to"] || "").scan(/<(.+?)>/).map { |x| sanitize_message_id x.first }
124
125 @replyto = Person.from_address header["reply-to"]
126 @list_address = if header["list-post"]
127 address = if header["list-post"] =~ /mailto:(.*?)[>\s$]/
128 $1
129 elsif header["list-post"] =~ /@/
130 header["list-post"] # just try the whole fucking thing
131 end
132 address && Person.from_address(address)
133 elsif header["mailing-list"]
134 address = if header["mailing-list"] =~ /list (.*?);/
135 $1
136 end
137 address && Person.from_address(address)
138 elsif header["x-mailing-list"]
139 Person.from_address header["x-mailing-list"]
140 end
141
142 @recipient_email = header["envelope-to"] || header["x-original-to"] || header["delivered-to"]
143 @source_marked_read = header["status"] == "RO"
144 @list_subscribe = header["list-subscribe"]
145 @list_unsubscribe = header["list-unsubscribe"]
146 end
147
148 ## Expected index entry format:
149 ## :message_id, :subject => String
150 ## :date => Time
151 ## :refs, :replytos => Array of String
152 ## :from => Person
153 ## :to, :cc, :bcc => Array of Person
154 def load_from_index! entry
155 @id = entry[:message_id]
156 @from = entry[:from]
157 @date = entry[:date]
158 @subj = entry[:subject]
159 @to = entry[:to]
160 @cc = entry[:cc]
161 @bcc = entry[:bcc]
162 @refs = (@refs + entry[:refs]).uniq
163 @replytos = entry[:replytos]
164
165 @replyto = nil
166 @list_address = nil
167 @recipient_email = nil
168 @source_marked_read = false
169 @list_subscribe = nil
170 @list_unsubscribe = nil
171 end
172
173 def add_ref ref
174 @refs << ref
175 @dirty = true
176 end
177
178 def remove_ref ref
179 @dirty = true if @refs.delete ref
180 end
181
182 attr_reader :snippet
183 def is_list_message?; !@list_address.nil?; end
184 def is_draft?; @labels.member? :draft; end
185 def draft_filename
186 raise "not a draft" unless is_draft?
187 source.fn_for_offset source_info
188 end
189
190 ## sanitize message ids by removing spaces and non-ascii characters.
191 ## also, truncate to 255 characters. all these steps are necessary
192 ## to make the index happy. of course, we probably fuck up a couple
193 ## valid message ids as well. as long as we're consistent, this
194 ## should be fine, though.
195 ##
196 ## also, mostly the message ids that are changed by this belong to
197 ## spam email.
198 ##
199 ## an alternative would be to SHA1 or MD5 all message ids on a regular basis.
200 ## don't tempt me.
201 def sanitize_message_id mid; mid.gsub(/(\s|[^\000-\177])+/, "")[0..254] end
202
203 def clear_dirty
204 @dirty = false
205 end
206
207 def has_label? t; @labels.member? t; end
208 def add_label l
209 l = l.to_sym
210 return if @labels.member? l
211 @labels << l
212 @dirty = true
213 end
214 def remove_label l
215 l = l.to_sym
216 return unless @labels.member? l
217 @labels.delete l
218 @dirty = true
219 end
220
221 def recipients
222 @to + @cc + @bcc
223 end
224
225 def labels= l
226 raise ArgumentError, "not a set" unless l.is_a?(Set)
227 raise ArgumentError, "not a set of labels" unless l.all? { |ll| ll.is_a?(Symbol) }
228 return if @labels == l
229 @labels = l
230 @dirty = true
231 end
232
233 def chunks
234 load_from_source!
235 @chunks
236 end
237
238 def location
239 @locations.find { |x| x.valid? } || raise(OutOfSyncSourceError.new)
240 end
241
242 def source
243 location.source
244 end
245
246 def source_info
247 location.info
248 end
249
250 ## this is called when the message body needs to actually be loaded.
251 def load_from_source!
252 @chunks ||=
253 begin
254 ## we need to re-read the header because it contains information
255 ## that we don't store in the index. actually i think it's just
256 ## the mailing list address (if any), so this is kinda overkill.
257 ## i could just store that in the index, but i think there might
258 ## be other things like that in the future, and i'd rather not
259 ## bloat the index.
260 ## actually, it's also the differentiation between to/cc/bcc,
261 ## so i will keep this.
262 rmsg = location.parsed_message
263 parse_header rmsg.header
264 message_to_chunks rmsg
265 rescue SourceError, SocketError, RMail::EncodingUnsupportedError => e
266 warn_with_location "problem reading message #{id}"
267 debug "could not load message, exception: #{e.inspect}"
268
269 [Chunk::Text.new(error_message.split("\n"))]
270
271 rescue Exception => e
272
273 warn_with_location "problem reading message #{id}"
274 debug "could not load message: #{location.inspect}, exception: #{e.inspect}"
275
276 raise e
277
278 end
279 end
280
281 def reload_from_source!
282 @chunks = nil
283 load_from_source!
284 end
285
286
287 def error_message
288 <<EOS
289 #@snippet...
290
291 ***********************************************************************
292 An error occurred while loading this message.
293 ***********************************************************************
294 EOS
295 end
296
297 def raw_header
298 location.raw_header
299 end
300
301 def raw_message
302 location.raw_message
303 end
304
305 def each_raw_message_line &b
306 location.each_raw_message_line(&b)
307 end
308
309 def sync_back
310 @locations.map { |l| l.sync_back @labels, self }.any? do
311 UpdateManager.relay self, :updated, self
312 end
313 end
314
315 def merge_labels_from_locations merge_labels
316 ## Get all labels from all locations
317 location_labels = Set.new([])
318
319 @locations.each do |l|
320 if l.valid?
321 location_labels = location_labels.union(l.labels?)
322 end
323 end
324
325 ## Add to the message labels the intersection between all location
326 ## labels and those we want to merge
327 location_labels = location_labels.intersection(merge_labels.to_set)
328
329 if not location_labels.empty?
330 @labels = @labels.union(location_labels)
331 @dirty = true
332 end
333 end
334
335 def indexable_body
336 indexable_chunks.map { |c| c.lines }.flatten.compact.join " "
337 end
338
339 def indexable_chunks
340 chunks.select { |c| c.indexable? } || []
341 end
342
343 def indexable_subject
344 Message.normalize_subj(subj)
345 end
346
347 def quotable_body_lines
348 chunks.find_all { |c| c.quotable? }.map { |c| c.lines }.flatten
349 end
350
351 def quotable_header_lines
352 ["From: #{@from.full_address}"] +
353 (@to.empty? ? [] : ["To: " + @to.map { |p| p.full_address }.join(", ")]) +
354 (@cc.empty? ? [] : ["Cc: " + @cc.map { |p| p.full_address }.join(", ")]) +
355 (@bcc.empty? ? [] : ["Bcc: " + @bcc.map { |p| p.full_address }.join(", ")]) +
356 ["Date: #{@date.rfc822}",
357 "Subject: #{@subj}"]
358 end
359
360 def self.build_from_source source, source_info
361 m = Message.new :locations => [Location.new(source, source_info)]
362 m.load_from_source!
363 m
364 end
365
366 private
367
368 ## here's where we handle decoding mime attachments. unfortunately
369 ## but unsurprisingly, the world of mime attachments is a bit of a
370 ## mess. as an empiricist, i'm basing the following behavior on
371 ## observed mail rather than on interpretations of rfcs, so probably
372 ## this will have to be tweaked.
373 ##
374 ## the general behavior i want is: ignore content-disposition, at
375 ## least in so far as it suggests something being inline vs being an
376 ## attachment. (because really, that should be the recipient's
377 ## decision to make.) if a mime part is text/plain, OR if the user
378 ## decoding hook converts it, then decode it and display it
379 ## inline. for these decoded attachments, if it has associated
380 ## filename, then make it collapsable and individually saveable;
381 ## otherwise, treat it as regular body text.
382 ##
383 ## everything else is just an attachment and is not displayed
384 ## inline.
385 ##
386 ## so, in contrast to mutt, the user is not exposed to the workings
387 ## of the gruesome slaughterhouse and sausage factory that is a
388 ## mime-encoded message, but need only see the delicious end
389 ## product.
390
391 def multipart_signed_to_chunks m
392 if m.body.size != 2
393 warn_with_location "multipart/signed with #{m.body.size} parts (expecting 2)"
394 return
395 end
396
397 payload, signature = m.body
398 if signature.multipart?
399 warn_with_location "multipart/signed with payload multipart #{payload.multipart?} and signature multipart #{signature.multipart?}"
400 return
401 end
402
403 ## this probably will never happen
404 if payload.header.content_type && payload.header.content_type.downcase == "application/pgp-signature"
405 warn_with_location "multipart/signed with payload content type #{payload.header.content_type}"
406 return
407 end
408
409 if signature.header.content_type && signature.header.content_type.downcase != "application/pgp-signature"
410 ## unknown signature type; just ignore.
411 #warn "multipart/signed with signature content type #{signature.header.content_type}"
412 return
413 end
414
415 [CryptoManager.verify(payload, signature), message_to_chunks(payload)].flatten.compact
416 end
417
418 def multipart_encrypted_to_chunks m
419 if m.body.size != 2
420 warn_with_location "multipart/encrypted with #{m.body.size} parts (expecting 2)"
421 return
422 end
423
424 control, payload = m.body
425 if control.multipart?
426 warn_with_location "multipart/encrypted with control multipart #{control.multipart?} and payload multipart #{payload.multipart?}"
427 return
428 end
429
430 if payload.header.content_type && payload.header.content_type.downcase != "application/octet-stream"
431 warn_with_location "multipart/encrypted with payload content type #{payload.header.content_type}"
432 return
433 end
434
435 if control.header.content_type && control.header.content_type.downcase != "application/pgp-encrypted"
436 warn_with_location "multipart/encrypted with control content type #{signature.header.content_type}"
437 return
438 end
439
440 notice, sig, decryptedm = CryptoManager.decrypt payload
441 if decryptedm # managed to decrypt
442 children = message_to_chunks(decryptedm, true)
443 [notice, sig].compact + children
444 else
445 [notice]
446 end
447 end
448
449 def has_embedded_message? m
450 return false unless m.header.content_type
451 %w(message/rfc822 message/global).include? m.header.content_type.downcase
452 end
453
454 ## takes a RMail::Message, breaks it into Chunk:: classes.
455 def message_to_chunks m, encrypted=false, sibling_types=[]
456 if m.multipart?
457 chunks =
458 case m.header.content_type.downcase
459 when "multipart/signed"
460 multipart_signed_to_chunks m
461 when "multipart/encrypted"
462 multipart_encrypted_to_chunks m
463 end
464
465 unless chunks
466 sibling_types = m.body.map { |p| p.header.content_type }
467 chunks = m.body.map { |p| message_to_chunks p, encrypted, sibling_types }.flatten.compact
468 end
469
470 chunks
471 elsif has_embedded_message? m
472 encoding = m.header["Content-Transfer-Encoding"]
473 if m.body
474 body =
475 case encoding
476 when "base64"
477 m.body.unpack("m")[0]
478 when "quoted-printable"
479 m.body.unpack("M")[0]
480 when "7bit", "8bit", nil
481 m.body
482 else
483 raise RMail::EncodingUnsupportedError, encoding.inspect
484 end
485 body = body.normalize_whitespace
486 payload = RMail::Parser.read(body)
487 from = payload.header.from.first ? payload.header.from.first.format : ""
488 to = payload.header.to.map { |p| p.format }.join(", ")
489 cc = payload.header.cc.map { |p| p.format }.join(", ")
490 subj = decode_header_field(payload.header.subject) || DEFAULT_SUBJECT
491 subj = Message.normalize_subj(subj.gsub(/\s+/, " ").gsub(/\s+$/, ""))
492 msgdate = payload.header.date
493 from_person = from ? Person.from_address(decode_header_field(from)) : nil
494 to_people = to ? Person.from_address_list(decode_header_field(to)) : nil
495 cc_people = cc ? Person.from_address_list(decode_header_field(cc)) : nil
496 [Chunk::EnclosedMessage.new(from_person, to_people, cc_people, msgdate, subj)] + message_to_chunks(payload, encrypted)
497 else
498 debug "no body for message/rfc822 enclosure; skipping"
499 []
500 end
501 elsif m.header.content_type && m.header.content_type.downcase == "application/pgp" && m.body
502 ## apparently some versions of Thunderbird generate encryped email that
503 ## does not follow RFC3156, e.g. messages with X-Enigmail-Version: 0.95.0
504 ## they have no MIME multipart and just set the body content type to
505 ## application/pgp. this handles that.
506 ##
507 ## TODO 1: unduplicate code between here and
508 ## multipart_encrypted_to_chunks
509 ## TODO 2: this only tries to decrypt. it cannot handle inline PGP
510 notice, sig, decryptedm = CryptoManager.decrypt m.body
511 if decryptedm # managed to decrypt
512 children = message_to_chunks decryptedm, true
513 [notice, sig].compact + children
514 else
515 ## try inline pgp signed
516 chunks = inline_gpg_to_chunks m.body, $encoding, (m.charset || $encoding)
517 if chunks
518 chunks
519 else
520 [notice]
521 end
522 end
523 else
524 filename =
525 ## first, paw through the headers looking for a filename.
526 ## RFC 2183 (Content-Disposition) specifies that disposition-parms are
527 ## separated by ";". So, we match everything up to " and ; (if present).
528 if m.header["Content-Disposition"] && m.header["Content-Disposition"] =~ /filename="?(.*?[^\\])("|;|\z)/m
529 $1
530 elsif m.header["Content-Type"] && m.header["Content-Type"] =~ /name="?(.*?[^\\])("|;|\z)/im
531 $1
532
533 ## haven't found one, but it's a non-text message. fake
534 ## it.
535 ##
536 ## TODO: make this less lame.
537 elsif m.header["Content-Type"] && m.header["Content-Type"] !~ /^text\/plain/i
538 extension =
539 case m.header["Content-Type"]
540 when /text\/html/ then "html"
541 when /image\/(.*)/ then $1
542 end
543
544 ["sup-attachment-#{Time.now.to_i}-#{rand 10000}", extension].join(".")
545 end
546
547 ## if there's a filename, we'll treat it as an attachment.
548 if filename
549 ## filename could be 2047 encoded
550 filename = Rfc2047.decode_to $encoding, filename
551 # add this to the attachments list if its not a generated html
552 # attachment (should we allow images with generated names?).
553 # Lowercase the filename because searches are easier that way
554 @attachments.push filename.downcase unless filename =~ /^sup-attachment-/
555 add_label :attachment unless filename =~ /^sup-attachment-/
556 content_type = (m.header.content_type || "application/unknown").downcase # sometimes RubyMail gives us nil
557 [Chunk::Attachment.new(content_type, filename, m, sibling_types)]
558
559 ## otherwise, it's body text
560 else
561 ## Decode the body, charset conversion will follow either in
562 ## inline_gpg_to_chunks (for inline GPG signed messages) or
563 ## a few lines below (messages without inline GPG)
564 body = m.body ? m.decode : ""
565
566 ## Check for inline-PGP
567 chunks = inline_gpg_to_chunks body, $encoding, (m.charset || $encoding)
568 return chunks if chunks
569
570 if m.body
571 ## if there's no charset, use the current encoding as the charset.
572 ## this ensures that the body is normalized to avoid non-displayable
573 ## characters
574 body = m.decode.transcode($encoding, m.charset)
575 else
576 body = ""
577 end
578
579 text_to_chunks(body.normalize_whitespace.split("\n"), encrypted)
580 end
581 end
582 end
583
584 ## looks for gpg signed (but not encrypted) inline messages inside the
585 ## message body (there is no extra header for inline GPG) or for encrypted
586 ## (and possible signed) inline GPG messages
587 def inline_gpg_to_chunks body, encoding_to, encoding_from
588 lines = body.split("\n")
589
590 # First case: Message is enclosed between
591 #
592 # -----BEGIN PGP SIGNED MESSAGE-----
593 # and
594 # -----END PGP SIGNED MESSAGE-----
595 #
596 # In some cases, END PGP SIGNED MESSAGE doesn't appear
597 # (and may leave strange -----BEGIN PGP SIGNATURE----- ?)
598 gpg = lines.between(GPG_SIGNED_START, GPG_SIGNED_END)
599 # between does not check if GPG_END actually exists
600 # Reference: http://permalink.gmane.org/gmane.mail.sup.devel/641
601 if !gpg.empty?
602 msg = RMail::Message.new
603 msg.body = gpg.join("\n")
604
605 body = body.transcode(encoding_to, encoding_from)
606 lines = body.split("\n")
607 sig = lines.between(GPG_SIGNED_START, GPG_SIG_START)
608 startidx = lines.index(GPG_SIGNED_START)
609 endidx = lines.index(GPG_SIG_END)
610 before = startidx != 0 ? lines[0 .. startidx-1] : []
611 after = endidx ? lines[endidx+1 .. lines.size] : []
612
613 # sig contains BEGIN PGP SIGNED MESSAGE and END PGP SIGNATURE, so
614 # we ditch them. sig may also contain the hash used by PGP (with a
615 # newline), so we also skip them
616 sig_start = sig[1].match(/^Hash:/) ? 3 : 1
617 sig_end = sig.size-2
618 payload = RMail::Message.new
619 payload.body = sig[sig_start, sig_end].join("\n")
620 return [text_to_chunks(before, false),
621 CryptoManager.verify(nil, msg, false),
622 message_to_chunks(payload),
623 text_to_chunks(after, false)].flatten.compact
624 end
625
626 # Second case: Message is encrypted
627
628 gpg = lines.between(GPG_START, GPG_END)
629 # between does not check if GPG_END actually exists
630 if !gpg.empty? && !lines.index(GPG_END).nil?
631 msg = RMail::Message.new
632 msg.body = gpg.join("\n")
633
634 startidx = lines.index(GPG_START)
635 before = startidx != 0 ? lines[0 .. startidx-1] : []
636 after = lines[lines.index(GPG_END)+1 .. lines.size]
637
638 notice, sig, decryptedm = CryptoManager.decrypt msg, true
639 chunks = if decryptedm # managed to decrypt
640 children = message_to_chunks(decryptedm, true)
641 [notice, sig].compact + children
642 else
643 [notice]
644 end
645 return [text_to_chunks(before, false),
646 chunks,
647 text_to_chunks(after, false)].flatten.compact
648 end
649 end
650
651 ## parse the lines of text into chunk objects. the heuristics here
652 ## need tweaking in some nice manner. TODO: move these heuristics
653 ## into the classes themselves.
654 def text_to_chunks lines, encrypted
655 state = :text # one of :text, :quote, or :sig
656 chunks = []
657 chunk_lines = []
658 nextline_index = -1
659
660 lines.each_with_index do |line, i|
661 if i >= nextline_index
662 # look for next nonblank line only when needed to avoid O(n²)
663 # behavior on sequences of blank lines
664 if nextline_index = lines[(i+1)..-1].index { |l| l !~ /^\s*$/ } # skip blank lines
665 nextline_index += i + 1
666 nextline = lines[nextline_index]
667 else
668 nextline_index = lines.length
669 nextline = nil
670 end
671 end
672
673 case state
674 when :text
675 newstate = nil
676
677 ## the following /:$/ followed by /\w/ is an attempt to detect the
678 ## start of a quote. this is split into two regexen because the
679 ## original regex /\w.*:$/ had very poor behavior on long lines
680 ## like ":a:a:a:a:a" that occurred in certain emails.
681 if line =~ QUOTE_PATTERN || (line =~ /:$/ && line =~ /\w/ && nextline =~ QUOTE_PATTERN)
682 newstate = :quote
683 elsif line =~ SIG_PATTERN && (lines.length - i) < MAX_SIG_DISTANCE && !lines[(i+1)..-1].index { |l| l =~ /^-- $/ }
684 newstate = :sig
685 elsif line =~ BLOCK_QUOTE_PATTERN && nextline !~ QUOTE_PATTERN
686 newstate = :block_quote
687 end
688
689 if newstate
690 chunks << Chunk::Text.new(chunk_lines) unless chunk_lines.empty?
691 chunk_lines = [line]
692 state = newstate
693 else
694 chunk_lines << line
695 end
696
697 when :quote
698 newstate = nil
699
700 if line =~ QUOTE_PATTERN || (line =~ /^\s*$/ && nextline =~ QUOTE_PATTERN)
701 chunk_lines << line
702 elsif line =~ SIG_PATTERN && (lines.length - i) < MAX_SIG_DISTANCE
703 newstate = :sig
704 else
705 newstate = :text
706 end
707
708 if newstate
709 if chunk_lines.empty?
710 # nothing
711 else
712 chunks << Chunk::Quote.new(chunk_lines)
713 end
714 chunk_lines = [line]
715 state = newstate
716 end
717
718 when :block_quote, :sig
719 chunk_lines << line
720 end
721
722 if !@have_snippet && state == :text && (@snippet.nil? || @snippet.length < SNIPPET_LEN) && line !~ /[=\*#_-]{3,}/ && line !~ /^\s*$/
723 @snippet ||= ""
724 @snippet += " " unless @snippet.empty?
725 @snippet += line.gsub(/^\s+/, "").gsub(/[\r\n]/, "").gsub(/\s+/, " ")
726 oldlen = @snippet.length
727 @snippet = @snippet[0 ... SNIPPET_LEN].chomp
728 @snippet += "..." if @snippet.length < oldlen
729 @dirty = true unless encrypted && $config[:discard_snippets_from_encrypted_messages]
730 @snippet_contains_encrypted_content = true if encrypted
731 end
732 end
733
734 ## final object
735 case state
736 when :quote, :block_quote
737 chunks << Chunk::Quote.new(chunk_lines) unless chunk_lines.empty?
738 when :text
739 chunks << Chunk::Text.new(chunk_lines) unless chunk_lines.empty?
740 when :sig
741 chunks << Chunk::Signature.new(chunk_lines) unless chunk_lines.empty?
742 end
743 chunks
744 end
745
746 def warn_with_location msg
747 warn msg
748 warn "Message is in #{@locations}"
749 end
750 end
751
752 class Location
753 attr_reader :source
754 attr_reader :info
755
756 def initialize source, info
757 @source = source
758 @info = info
759 end
760
761 def raw_header
762 source.raw_header info
763 end
764
765 def raw_message
766 source.raw_message info
767 end
768
769 def sync_back labels, message
770 synced = false
771 return synced unless sync_back_enabled? and valid?
772 source.synchronize do
773 new_info = source.sync_back(@info, labels)
774 if new_info
775 @info = new_info
776 Index.sync_message message, true
777 synced = true
778 end
779 end
780 synced
781 end
782
783 def sync_back_enabled?
784 source.respond_to? :sync_back and $config[:sync_back_to_maildir] and source.sync_back_enabled?
785 end
786
787 ## much faster than raw_message
788 def each_raw_message_line &b
789 source.each_raw_message_line info, &b
790 end
791
792 def parsed_message
793 source.load_message info
794 end
795
796 def fallback_date
797 source.fallback_date_for_message info
798 end
799
800 def valid?
801 source.valid? info
802 end
803
804 def labels?
805 source.labels? info
806 end
807
808 def == o
809 o.source.id == source.id and o.info == info
810 end
811
812 def hash
813 [source.id, info].hash
814 end
815 end
816
817 end