mailr/lib/webmail/mail2screen.rb

172 lines
6.4 KiB
Ruby
Raw Normal View History

require 'cdfutils'
module Mail2Screen
def mail2html(mail, msg_id)
footer = ""
parsed_body = create_body(mail, msg_id, footer)
2011-06-18 13:27:44 +02:00
ret = "<table class='messageheader' border='0' cellpadding='0' cellspacing='0' >\n"
ret << "<tbody>\n"
2009-09-01 14:23:05 +02:00
ret << " <tr><td class='label' nowrap='nowrap'>#{t :from}:</td><td>#{address(mail.from_addrs, @msg_id)}</td></tr>\n"
ret << " <tr><td class='label' nowrap='nowrap'>#{t :to}:</td><td>#{address(mail.to_addrs, @msg_id)}</td></tr>\n"
if @mail.cc_addrs
2009-09-01 14:23:05 +02:00
ret << " <tr><td class='label' nowrap='nowrap'>#{t :cc}:</td><td>#{address(mail.cc_addrs, @msg_id)}</td></tr>\n"
end
if @mail.bcc_addrs
2009-09-01 14:23:05 +02:00
ret << " <tr><td class='label' nowrap='nowrap'>#{t :bcc}:</td><td>#{address(mail.bcc_addrs, @msg_id)}</td></tr>\n"
end
2011-06-18 13:27:44 +02:00
ret << " <tr><td class='label' nowrap='nowrap'>#{t :subject}:</td><td>#{h(mime_encoded?(mail.subject) ? mime_decode(mail.subject) : mail.subject)}</dd>\n"
2009-09-01 14:23:05 +02:00
ret << " <tr><td class='label' nowrap='nowrap'>#{t :date}:</td><td>#{h message_date(mail.date)}</td></tr>\n"
if footer != ''
ret << " <tr><td class='label' nowrap='nowrap'>#{image_tag('attachment.png')}</td><td>#{footer}</td></tr>\n"
end
ret << " </tbody>\n"
ret << "</table>\n"
ret << "<div class='msgpart'>\n"
ret << parsed_body
ret << "</div>\n"
end
2011-06-18 13:27:44 +02:00
def create_body(mail, msg_id, footer)
charset = (mail.charset.nil? ? 'iso-8859-1' : mail.charset)
if mail.multipart?
ret = ""
if mail.content_type == 'multipart/alternative'
# take only HTML part
mail.parts.each { |part|
if part.content_type == "text/html" or part.multipart?
ret << create_body(part, msg_id, footer)
end
}
2011-06-18 13:27:44 +02:00
return ret
else
mail.parts.each { |part|
if part.multipart?
ret << create_body(part, msg_id, footer)
else
footer << ", " if footer != ''
footer << add_attachment(part.header['content-type'], msg_id)
if part.content_type == "text/plain" or part.content_type.nil?
charset = (part.charset.nil? ? charset : mail.charset)
ret << add_text(part, part.transfer_encoding, charset)
elsif part.content_type == "text/html"
charset = (part.charset.nil? ? charset : mail.charset)
ret << add_html(part, part.transfer_encoding, charset)
elsif part.content_type.include?("image/")
ctype = part.header['content-type']
ret << add_image(ctype, msg_id)
2011-06-18 13:27:44 +02:00
elsif part.content_type.include?("message/rfc822")
ret << "<br/>#{_('Follows attached message')}:<hr/>" << mail2html(TMail::Mail.parse(part.body), msg_id)
end
end
}
return ret
2011-06-18 13:27:44 +02:00
end
else
ret = ""
if mail.content_type == "text/plain" or mail.content_type.nil?
ret << add_text(mail, mail.transfer_encoding, charset)
elsif mail.content_type == "text/html"
ret << add_html(mail, mail.transfer_encoding, charset)
end
return ret
end
end
2011-06-18 13:27:44 +02:00
def add_text(part, encoding, charset)
2011-06-18 13:27:44 +02:00
CGI.escapeHTML(decode_part_text("#{part}", encoding, charset)).gsub(/\r\n/,"<br/>").gsub(/\r/, "<br/>").gsub(/\n/,"<br/>")
end
2011-06-18 13:27:44 +02:00
def add_html(part, encoding, charset)
strip_html(decode_part_text("#{part}", encoding, charset))
end
2011-06-18 13:27:44 +02:00
def decode_part_text(part_str, encoding, charset)
# Parse mail
header, text = "", ""
2011-06-18 13:27:44 +02:00
# Get header and body
#Content-type: text/plain; charset="ISO-8859-1"
2011-06-18 13:27:44 +02:00
#Content-transfer-encoding: quoted-printable
isBody = false
2011-06-18 13:27:44 +02:00
part_str.each_line { |line|
if isBody
text << line
else
if line.strip == ""
isBody = true
else
header << line
end
end
}
# Manage encoding
if not(encoding.nil?) and encoding.downcase == "quoted-printable"
ret = from_qp(text)
elsif not(encoding.nil?) and encoding.downcase == "base64"
ret = "#{text.unpack("m")}"
2011-06-18 13:27:44 +02:00
else
ret = text
end
# manage charset
if ret.nil? or charset.nil? or charset.downcase == "utf-8"
return ret
else
begin
return Iconv.conv("UTF-8",charset.downcase, ret)
rescue Exception => ex
RAILS_DEFAULT_LOGGER.debug("Exception occured #{ex}\n#{ex.backtrace.join('\n')}")
return ret
end
2011-06-18 13:27:44 +02:00
end
end
2011-06-18 13:27:44 +02:00
def add_attachment(content_type, msg_id)
filename = (content_type.nil? or content_type['name'].nil? ? "" : content_type['name'])
if filename == ""
""
else
2009-02-07 21:18:14 +01:00
"<span class='attachment'>&nbsp;<a href='/webmail/download?msg_id=#{msg_id}&ctype=" << CGI.escape(filename) << "'>#{filename}</a></span>"
end
end
2011-06-18 13:27:44 +02:00
def add_image(content_type, msg_id)
filename = (content_type.nil? or content_type['name'].nil? ? "" : content_type['name'])
2009-02-07 21:18:14 +01:00
"<hr/><span class='attachment'><br/><img src='/webmail/download?msg_id=#{msg_id}&ctype=" << CGI.escape(filename) << "' alt='#{filename}'/></span>"
end
2011-06-18 13:27:44 +02:00
def friendly_address(addr)
addr.kind_of?(Net::IMAP::Address) ? ((addr.name.nil? or addr.name.strip == "") ? "#{addr.mailbox}@#{addr.host}" : "#{(mime_encoded?(addr.name.strip) ? mime_decode(addr.name.to_s): addr.name.to_s)}<#{addr.mailbox}@#{addr.host}>") : ((addr.name.nil? or addr.name.strip == "") ? "#{addr.spec}" : "#{(mime_encoded?(addr.name.strip) ? mime_decode(addr.name.to_s): addr.name.to_s)}<#{addr.spec}>")
end
2011-06-18 13:27:44 +02:00
def friendly_address_or_name(addr)
2011-06-18 13:27:44 +02:00
if addr.kind_of?(Net::IMAP::Address)
((addr.name.nil? or addr.name.to_s == "") ? "#{addr.mailbox}@#{addr.host}" : (mime_encoded?(addr.name.to_s) ? mime_decode(addr.name.to_s): addr.name.to_s))
else
((addr.nil? or addr.to_s == "") ? "#{addr.to_s}" : (mime_encoded?(addr.to_s) ? mime_decode(addr.to_s): addr.to_s))
end
end
2011-06-18 13:27:44 +02:00
def add_to_contact(addr, msg_id)
2011-06-18 13:27:44 +02:00
"&nbsp;<a href='/contact/add_from_mail?cstr=#{CGI.escape(friendly_address(addr))}&retmsg=#{msg_id}'>"+t(:add_to_contacts)
end
2011-06-18 13:27:44 +02:00
def short_address(addresses)
ret = ""
addresses.each { |addr| #split(/,\s*/)
ret << "," unless ret == ""
ret << CGI.escapeHTML(friendly_address_or_name(addr))
} unless addresses.nil?
ret
end
2011-06-18 13:27:44 +02:00
def address(addresses, msg_id)
ret = ""
addresses.each { |addr| #split(/,\s*/)
ret << "," unless ret == ""
ret << CGI.escapeHTML(friendly_address_or_name(addr)) << add_to_contact(addr, msg_id)
} unless addresses.nil?
return ret
end
2009-02-07 21:18:14 +01:00
end