require 'cdfutils' module Mail2Screen def mail2html(mail, msg_id) footer = "" parsed_body = create_body(mail, msg_id, footer) ret = "\n" ret << "\n" ret << " \n" ret << " \n" if @mail.cc_addrs ret << " \n" end if @mail.bcc_addrs ret << " \n" end ret << " \n" if footer != '' ret << " \n" end ret << " \n" ret << "
#{_('From')}:#{address(mail.from_addrs, @msg_id)}
#{_('To')}:#{address(mail.to_addrs, @msg_id)}
#{_('CC')}:#{address(mail.cc_addrs, @msg_id)}
#{_('BCC')}:#{address(mail.bcc_addrs, @msg_id)}
#{_('Subject')}:#{h(mime_encoded?(mail.subject) ? mime_decode(mail.subject) : mail.subject)}\n" ret << "
#{_('Date')}:#{h message_date(mail.date)}
#{image_tag('attachment.png')}#{footer}
\n" ret << "
\n" ret << parsed_body ret << "
\n" end 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 } 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) elsif part.content_type.include?("message/rfc822") ret << "
#{_('Follows attached message')}:
" << mail2html(TMail::Mail.parse(part.body), msg_id) end end } return ret 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 def add_text(part, encoding, charset) CGI.escapeHTML(decode_part_text("#{part}", encoding, charset)).gsub(/\r\n/,"
").gsub(/\r/, "
").gsub(/\n/,"
") end def add_html(part, encoding, charset) strip_html(decode_part_text("#{part}", encoding, charset)) end def decode_part_text(part_str, encoding, charset) # Parse mail header, text = "", "" # Get header and body #Content-type: text/plain; charset="ISO-8859-1" #Content-transfer-encoding: quoted-printable isBody = false 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")}" 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 end end def add_attachment(content_type, msg_id) filename = (content_type.nil? or content_type['name'].nil? ? "" : content_type['name']) if filename == "" "" else " #{filename}" end end def add_image(content_type, msg_id) filename = (content_type.nil? or content_type['name'].nil? ? "" : content_type['name']) "

#{filename}
" end 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 def friendly_address_or_name(addr) 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)) : ((addr.name.nil? or addr.name.to_s == "") ? "#{addr.spec}" : (mime_encoded?(addr.name.to_s) ? mime_decode(addr.name.to_s): addr.name.to_s)) end def add_to_contact(addr, msg_id) " Add to contacts" end 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 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 end