Module: Exjournal
- Defined in:
- lib/exjournal.rb
Class Method Summary collapse
-
.discard_mail_body(content) ⇒ Object
discard everything after the first nn , i.e.
-
.extract_journalled_mail(mail, strip_content = true) ⇒ Object
extract the first message/rfc822 attachment from the RFC822 encoded content, and return it as a TMail::Mail if
headers_only
is true then message content will be discarded, and only headers processed. - .logger ⇒ Object
- .logger=(l) ⇒ Object
-
.mail_to_hash(mail, strip_content = true) ⇒ Object
turn a TMail::Mail into a has suitable for JSON representation.
-
.parse_address(address) ⇒ Object
parse an address to a hash.
-
.parse_addresses(addresses) ⇒ Object
parse one or more addresses to hash.
-
.strip_header(header) ⇒ Object
remove angle brackets from a header string.
-
.strip_headers(headers) ⇒ Object
remove angle brackets from one or more headers.
- .with_headers(headers) ⇒ Object
Class Method Details
.discard_mail_body(content) ⇒ Object
discard everything after the first nn , i.e. all message body content from an RFC822 encoded mail
34 35 36 |
# File 'lib/exjournal.rb', line 34 def discard_mail_body(content) content.gsub(/^(.*?)\r\n\r\n.*$/m, '\1') end |
.extract_journalled_mail(mail, strip_content = true) ⇒ Object
extract the first message/rfc822 attachment from the RFC822 encoded content, and return it as a TMail::Mail if headers_only
is true then message content will be discarded, and only headers processed
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/exjournal.rb', line 19 def extract_journalled_mail(mail, strip_content=true) journal_mail = TMail::Mail.parse(mail) if mail.is_a?(String) # get the attachment = journal_mail.parts.select{ |p| p.content_disposition == "attachment" || p.content_type == "message/rfc822" }.first # complain if the email has no attachment to extract raise "attempted to extract journalled mail, but message has no attachments: \n#{mail}\n\n" unless mail_content = strip_content ? discard_mail_body(.body) : .body TMail::Mail.parse(mail_content) end |
.logger ⇒ Object
10 11 12 |
# File 'lib/exjournal.rb', line 10 def logger @logger ||= Logger.new($stderr) end |
.logger=(l) ⇒ Object
6 7 8 |
# File 'lib/exjournal.rb', line 6 def logger=(l) @logger=l end |
.mail_to_hash(mail, strip_content = true) ⇒ Object
turn a TMail::Mail into a has suitable for JSON representation
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/exjournal.rb', line 72 def mail_to_hash(mail, strip_content=true) mail = TMail::Mail.parse(mail) if mail.is_a?(String) = strip_header(mail.) if mail. sent_at = mail.date.xmlschema in_reply_to = strip_headers(mail.in_reply_to) if mail.in_reply_to references = strip_headers(mail.references) if mail.references from = parse_addresses(mail.from_addrs).first sender = parse_addresses(mail.sender).first to = parse_addresses(mail.to_addrs) cc=parse_addresses(mail.cc_addrs) bcc=parse_addresses(mail.bcc_addrs) h = { :message_id=>, :sent_at=>sent_at, :in_reply_to=>in_reply_to, :references=>references, :from=>from, :sender=>sender, :to=>to, :cc=>cc, :bcc=>bcc } if !strip_content h[:subject] = mail.subject end h end |
.parse_address(address) ⇒ Object
parse an address to a hash
53 54 55 56 |
# File 'lib/exjournal.rb', line 53 def parse_address(address) address = TMail::Address.parse(address) if address.is_a?(String) {:name=>address.name, :email_address=>address.address} end |
.parse_addresses(addresses) ⇒ Object
parse one or more addresses to hash. failures result in a warning logged
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/exjournal.rb', line 59 def parse_addresses(addresses) [*(addresses||[])].map do |a| begin parse_address(a) rescue Exception=>e logger.warn("failure parsing: #{a}") logger.warn(e) nil end end.compact end |
.strip_header(header) ⇒ Object
remove angle brackets from a header string
39 40 41 |
# File 'lib/exjournal.rb', line 39 def strip_header(header) header.gsub(/^<(.*)>$/, '\1') end |
.strip_headers(headers) ⇒ Object
remove angle brackets from one or more headers
48 49 50 |
# File 'lib/exjournal.rb', line 48 def strip_headers(headers) with_headers(headers){|h| strip_header(h)} end |
.with_headers(headers) ⇒ Object
43 44 45 |
# File 'lib/exjournal.rb', line 43 def with_headers(headers) [*(headers||[])].map{|h| yield h} end |