class Glima::Resource::Mail

Public Class Methods

new(message) click to toggle source
Calls superclass method
# File lib/glima/resource/mail.rb, line 18
def initialize(message)
  if message.respond_to?(:raw)
    @gmail_message = message
    super(message.raw)
  else
    super(message)
  end
end
read(mail_filename) click to toggle source
# File lib/glima/resource/mail.rb, line 14
def self.read(mail_filename)
  new(File.open(mail_filename, 'rb') {|f| f.read })
end

Public Instance Methods

find_passwordish_strings() click to toggle source
# File lib/glima/resource/mail.rb, line 50
def find_passwordish_strings
  mail = self
  body = mail.to_plain_text

  password_candidates = []

  # gather passwordish ASCII strings.
  body.scan(/(?:^|[^!-~])([!-~]{4,16})[^!-~]/) do |str|
    password_candidates += str
  end
  return password_candidates
end
format_mew(count = nil) click to toggle source
# File lib/glima/resource/mail.rb, line 87
def format_mew(count = nil)
  date = Time.at(internal_date.to_i/1000).strftime("%m/%d ")

  mark1 = " "
  mark1 = "U" if label_ids.include?("UNREAD")

  mark2 = " "
  mark2 = "-" if content_type =~ /multipart\/alternative/
  mark2 = "M" unless attachments.empty?

  folder = File.expand_path("~/Mail/all") # XXX

  summary = "#{mark1}#{mark2}#{date} #{CGI.unescapeHTML(snippet)}"
  summary += "\r +#{folder} #{id} <#{id}>"
  summary += if id != thread_id then " <#{thread_id}>" else " " end

  return summary + " 1 2"
end
format_summary(count = nil) click to toggle source
# File lib/glima/resource/mail.rb, line 81
def format_summary(count = nil)
  date = Time.at(internal_date.to_i/1000).strftime("%Y/%m/%d %H:%M")
  count = if count then ("%4d " % count) else "" end
  return "#{count}#{date} #{id} #{CGI.unescapeHTML(snippet)[0..30]}"
end
gm_label_ids() click to toggle source
# File lib/glima/resource/mail.rb, line 37
def gm_label_ids
  @gmail_message&.label_ids
end
Also aliased as: label_ids
gm_msgid() click to toggle source
# File lib/glima/resource/mail.rb, line 27
def gm_msgid
  @gmail_message&.id
end
Also aliased as: id
gm_thrid() click to toggle source
# File lib/glima/resource/mail.rb, line 32
def gm_thrid
  @gmail_message&.thread_id
end
Also aliased as: thread_id
id()
Alias for: gm_msgid
label_ids()
Alias for: gm_label_ids
raw() click to toggle source
# File lib/glima/resource/mail.rb, line 42
def raw
  @gmail_message&.raw
end
thread_id()
Alias for: gm_thrid
to_plain_text() click to toggle source
# File lib/glima/resource/mail.rb, line 46
def to_plain_text
  mail_to_plain_text(self)
end
unlock_zip!(password_candidates = [""], logger = nil) click to toggle source
# File lib/glima/resource/mail.rb, line 63
def unlock_zip!(password_candidates = [""], logger = nil)
  # Unlock all zip attachments in mail
  transformed = false

  self.attachments.each do |attachment|
    next unless attachment.filename =~ /\.zip$/i

    zip = Glima::Zip.new(attachment.body.decoded)
    # try all passwords
    if zip.unlock_password!(password_candidates, logger)
      attachment.body = zip.to_decrypted_unicode_zip.to_base64
      attachment.content_transfer_encoding = "base64"
      transformed = true
    end
  end
  return transformed
end

Private Instance Methods

convert_to_utf8(string, from_charset = nil) click to toggle source
# File lib/glima/resource/mail.rb, line 134
def convert_to_utf8(string, from_charset = nil)
  if from_charset && from_charset != "utf-8"
    string.encode("utf-8", from_charset,
                  :invalid => :replace, :undef => :replace)
  else
    string.force_encoding("utf-8")
  end
end
mail_to_plain_text(mail) click to toggle source
# File lib/glima/resource/mail.rb, line 108
def mail_to_plain_text(mail)
  parts = if mail.multipart? then mail.parts else [mail] end

  body = parts.map do |part|
    part_to_plain_text(part)
  end.join("----PART----PART----PART----PART----PART----\n")

  return pretty_hearder + "\n" + body
end
part_to_plain_text(part) click to toggle source
# File lib/glima/resource/mail.rb, line 118
def part_to_plain_text(part)
  case part.content_type
  when /text\/plain/
    convert_to_utf8(part.body.decoded.to_s,
                    part.content_type_parameters["charset"])
  when /multipart\/alternative/
    part_to_plain_text(part.text_part)

  when /message\/rfc822/
    mail_to_plain_text(::Mail.new(part.body.decoded.to_s))

  else
    "NOT_TEXT_PART (#{part.content_type})\n"
  end
end
pretty_hearder() click to toggle source
# File lib/glima/resource/mail.rb, line 143
def pretty_hearder
  mail = self
  ["Subject: #{mail.subject}",
   "From: #{mail.header['from']&.decoded}",
   "Date: #{mail.header['date']}",
   "Message-Id: #{mail.header['message_id']}",
   "To: #{mail.header['to']&.decoded}",
   "Cc: #{mail.header['cc']&.decoded}"
  ].join("\n") + "\n"
end