Class: Vmail::MessageFormatter
- Inherits:
-
Object
- Object
- Vmail::MessageFormatter
- Defined in:
- lib/vmail/message_formatter.rb
Instance Method Summary collapse
- #encoding ⇒ Object
- #extract_headers(mail = @mail) ⇒ Object
-
#find_text_part2(part, content_type) ⇒ Object
helper method.
-
#format_html_body(part) ⇒ Object
depend on lynx or whatever is set by the VMAIL_HTML_PART_READER variable.
- #format_part(part) ⇒ Object
- #format_text_body(part) ⇒ Object
-
#initialize(mail, uid = nil) ⇒ MessageFormatter
constructor
initialize with a Mail object.
- #list_parts(parts = (@mail.parts.empty? ? [@mail] : @mail.parts)) ⇒ Object
- #plaintext_part(mail = @mail) ⇒ Object
- #utf8(string, this_encoding = encoding) ⇒ Object
Constructor Details
#initialize(mail, uid = nil) ⇒ MessageFormatter
initialize with a Mail object
8 9 10 11 |
# File 'lib/vmail/message_formatter.rb', line 8 def initialize(mail, uid = nil) @mail = mail @uid = uid end |
Instance Method Details
#encoding ⇒ Object
104 105 106 |
# File 'lib/vmail/message_formatter.rb', line 104 def encoding @encoding ||= @mail.header.charset || 'UTF-8' end |
#extract_headers(mail = @mail) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/vmail/message_formatter.rb', line 87 def extract_headers(mail = @mail) headers = {'from' => utf8(mail['from'].decoded), 'date' => (mail.date.strftime('%a, %b %d %I:%M %p %Z %Y') rescue mail.date), 'to' => mail['to'].nil? ? nil : utf8(mail['to'].decoded), 'subject' => utf8(mail.subject) } if !mail.cc.nil? headers['cc'] = utf8(mail['cc'].decoded.to_s) end if !mail.reply_to.nil? headers['reply_to'] = utf8(mail['reply_to'].decoded) end headers rescue {'error' => $!} end |
#find_text_part2(part, content_type) ⇒ Object
helper method
35 36 37 38 39 40 41 42 43 |
# File 'lib/vmail/message_formatter.rb', line 35 def find_text_part2(part, content_type) if part.multipart? part.parts.map {|p| find_text_part2(p, p.content_type)}.compact.first elsif content_type =~ %r[^text/plain] || content_type =~ %r[text/plain] || content_type =~ %r[message/rfc] part end end |
#format_html_body(part) ⇒ Object
depend on lynx or whatever is set by the VMAIL_HTML_PART_READER variable
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/vmail/message_formatter.rb', line 72 def format_html_body(part) html_tool = ENV['VMAIL_HTML_PART_READER'] || 'lynx -stdin -dump' html = part.body.decoded.gsub("\r", '') stdin, stdout, stderr = Open3.popen3(html_tool) stdin.puts html stdin.close output = "[vmail: html part translated into plaintext by '#{html_tool}']\n\n" + stdout.read charset = part.content_type_parameters && part.content_type_parameters['charset'] if charset && charset != 'UTF-8' Iconv.conv('UTF-8//TRANSLIT//IGNORE', charset, output) else output end end |
#format_part(part) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/vmail/message_formatter.rb', line 45 def format_part(part) if part && part.respond_to?(:header) case part.header["Content-Type"].to_s when /text\/html/ format_html_body(part) when /text\/plain/ format_text_body(part) when /message\/rfc/ m = Mail.new(part.body.decoded) plaintext_part(m) else # just format_text on it anyway format_text_body(part) end else part.decoded.gsub("\r", '') end rescue puts $! "[error:] #{$!}" end |
#format_text_body(part) ⇒ Object
66 67 68 |
# File 'lib/vmail/message_formatter.rb', line 66 def format_text_body(part) part.body.decoded.gsub("\r", '') end |
#list_parts(parts = (@mail.parts.empty? ? [@mail] : @mail.parts)) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/vmail/message_formatter.rb', line 13 def list_parts(parts = (@mail.parts.empty? ? [@mail] : @mail.parts)) lines = parts.map do |part| if part.multipart? list_parts(part.parts) else # part.charset could be used "- #{part.content_type}" end end lines.flatten end |
#plaintext_part(mail = @mail) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/vmail/message_formatter.rb', line 25 def plaintext_part(mail=@mail) part = find_text_part2(mail.body, mail.content_type) if part.nil? "[No message body]" else format_part part end end |
#utf8(string, this_encoding = encoding) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/vmail/message_formatter.rb', line 108 def utf8(string, this_encoding = encoding) return '' unless string out = if this_encoding && this_encoding.upcase != 'UTF-8' Iconv.conv('UTF-8//TRANSLIT/IGNORE', this_encoding, string) elsif this_encoding.upcase == 'UTF-8' string else # assume UTF-8 Iconv.conv('US-ASCII//TRANSLIT/IGNORE', 'UTF-8', string) end out rescue "[error: #$!]" end |