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_part(parts = @mail.parts) ⇒ Object
-
#format_html_body(part) ⇒ Object
depend on lynx.
- #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
- #process_body ⇒ Object
- #utf8(string) ⇒ 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
102 103 104 |
# File 'lib/vmail/message_formatter.rb', line 102 def encoding @encoding ||= @mail.header.charset || 'utf-8' end |
#extract_headers(mail = @mail) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/vmail/message_formatter.rb', line 85 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_part(parts = @mail.parts) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/vmail/message_formatter.rb', line 46 def find_text_part(parts = @mail.parts) if parts.empty? return @mail end part = parts.detect {|part| part.multipart?} if part find_text_part(part.parts) else # no multipart part part = parts.detect {|part| (part.header["Content-Type"].to_s =~ /text\/plain/) } if part return part else parts.first end end end |
#format_html_body(part) ⇒ Object
depend on lynx
75 76 77 78 79 80 81 82 83 |
# File 'lib/vmail/message_formatter.rb', line 75 def format_html_body(part) html = part.body.decoded.gsub("\r", '') stdin, stdout, stderr = Open3.popen3("lynx -stdin -dump") stdin.puts html stdin.close output = stdout.read charset = part.content_type_parameters && part.content_type_parameters['charset'] charset ? Iconv.conv('utf-8//translit//ignore', charset, output) : output end |
#format_text_body(part) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/vmail/message_formatter.rb', line 64 def format_text_body(part) text = part.body.decoded.gsub("\r", '') charset = part.content_type_parameters && part.content_type_parameters['charset'] if charset Iconv.conv('utf-8//translit//ignore', charset, text) else text end end |
#list_parts(parts = (@mail.parts.empty? ? [@mail] : @mail.parts)) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/vmail/message_formatter.rb', line 13 def list_parts(parts = (@mail.parts.empty? ? [@mail] : @mail.parts)) if parts.empty? return [] end 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 |
#process_body ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/vmail/message_formatter.rb', line 28 def process_body part = find_text_part(@mail.parts) body = if part && part.respond_to?(:header) if part.header["Content-Type"].to_s =~ /text\/plain/ format_text_body(part) elsif part.header["Content-Type"].to_s =~ /text\/html/ format_html_body(part) else format_text_body(part) end else "NO BODY" end rescue puts $! body end |
#utf8(string) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/vmail/message_formatter.rb', line 106 def utf8(string) return '' unless string return string unless encoding Iconv.conv('utf-8//translit/ignore', encoding, string) rescue puts $! string end |