Class: BodyParts
- Inherits:
-
Object
- Object
- BodyParts
- Defined in:
- lib/bodyparts.rb
Class Method Summary collapse
- .extract_mail_attributes(mail_object) ⇒ Object
- .extract_tmail_attributes(tmail_object) ⇒ Object
- .find_reply_in(email) ⇒ Object
- .rules ⇒ Object
Class Method Details
.extract_mail_attributes(mail_object) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bodyparts.rb', line 28 def self.extract_mail_attributes(mail_object) if plain_part = mail_object.find_first_mime_type('text/plain') part = plain_part else part = mail_object end if mail_encoding = part['content_transfer_encoding'] || mail_encoding = mail_object['content_transfer_encoding'] content_encoding = mail_encoding.encoding else content_encoding = "not known" end {:content_encoding => content_encoding, :body => part.body.raw_source} end |
.extract_tmail_attributes(tmail_object) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bodyparts.rb', line 12 def self.extract_tmail_attributes(tmail_object) if mail_encoding = tmail_object['content_transfer_encoding'] content_encoding = mail_encoding.to_s.downcase else content_encoding = "not known" end body = if tmail_object.multipart? tmail_object.parts.first.body else tmail_object.body end {:content_encoding => content_encoding, :body => body} end |
.find_reply_in(email) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bodyparts.rb', line 44 def self.find_reply_in(email) email = Mail::Message.new(email) if email.is_a? String mail_attributes = case email.class.to_s when "TMail::Mail" then extract_tmail_attributes(email) when "Mail::Message" then extract_mail_attributes(email) else raise "You must pass in either a TMail or Mail object or raw email source text" end raw_body = mail_attributes[:body] body = case mail_attributes[:content_encoding] when "base64" then Base64.decode64 raw_body when "quoted-printable" then Mail::Encodings::QuotedPrintable.decode raw_body else raw_body end matches = rules.map {|rule| body.match(rule[:reply_delimiter])}.compact! unless matches.empty? match = matches.sort_by {|m| m.begin(0)}.first = body[0, match.begin(0)] {:new_message => .strip, :rest_of_thread => body[match.begin(0)..-1].strip} else {:new_message => body, :rest_of_thread => nil} end end |
.rules ⇒ Object
5 6 7 8 9 10 |
# File 'lib/bodyparts.rb', line 5 def self.rules [{ :server => 'Gmail', :reply_delimiter => /^On.*?wrote:.$/m }, { :server => 'Yahoo! Mail', :reply_delimiter => /^_+\r\nFrom:/ }, { :server => 'Microsoft Live Mail/Hotmail', :reply_delimiter => /\r\n\r\n(Date|Subject):/ }, { :server => 'Outlook Express/AOL Webmail', :reply_delimiter => /^-+.*Original Message.*-+/ }] end |