class Glima::Command::Dezip

Public Class Methods

new(gmail_id, directory, password_file = nil, password_dir = nil) click to toggle source
# File lib/glima/command/dezip.rb, line 5
def initialize(gmail_id, directory, password_file = nil, password_dir = nil)

  unless File.writable?(File.expand_path(directory))
    logger.error "#{directory} is not writable."
    exit 1
  end

  mail = client.get_user_smart_message(gmail_id) do |m, err|
    exit_if_error(gmail_id, err, logger)
  end

  # get password candidates from config file
  password_candidates = []
  password_file = File.expand_path(password_file)
  if File.exists?(password_file)
    password_candidates += File.open(password_file) {|f| f.read.split(/\n+/) }
  end

  # gather password candidates from nearby mails
  index = 0
  client.nearby_mails(mail) do |nm|
    index += 1
    logger.info "Passwordish mail(#{index}): " + nm.format_summary
    password_candidates += nm.find_passwordish_strings
  end

  # try to unlock zip attachments
  unless mail.unlock_zip!(password_candidates, logger)
    logger.info "Password unlock failed."
    return false
  end

  # Write to unlocked zip file to DIRECTORY
  mail.attachments.each do |attachment|
    next unless attachment.filename =~ /\.zip$/i
    zip_filename = File.expand_path(attachment.filename, directory)
    Glima::Zip.new(attachment.body.decoded).write_to_file(zip_filename)
    logger.info "Wrote to #{zip_filename || 'STDOUT'}."
  end
end