class Sakura::Cli::Mail

Public Instance Methods

create(local_part, password = nil) click to toggle source
# File lib/sakura/cli/mail.rb, line 26
def create(local_part, password = nil)
  preprocess

  password ||= ask_password

  begin
    MailAddress.create local_part, password
  rescue StandardError
    raise if options[:verbose]

    abort $ERROR_INFO
  end
end
delete(local_part) click to toggle source
# File lib/sakura/cli/mail.rb, line 42
def delete(local_part)
  preprocess

  begin
    find(local_part).delete
  rescue StandardError
    raise if options[:verbose]

    abort $ERROR_INFO
  end
end
filter(local_part, value = nil) click to toggle source
# File lib/sakura/cli/mail.rb, line 168
def filter(local_part, value = nil)
  preprocess

  self.class.handle_argument_error if value && value !~ /mark|disable|discard|quarantine/

  mail = find(local_part)

  begin
    case value
    when nil
      puts mail.spam_filter
    else
      mail.spam_filter = value.to_sym
    end
  rescue StandardError
    raise if options[:verbose]

    abort $ERROR_INFO
  end
end
forward(local_part, operation = nil, mail_to_forward = nil) click to toggle source
# File lib/sakura/cli/mail.rb, line 118
def forward(local_part, operation = nil, mail_to_forward = nil)
  preprocess

  self.class.handle_argument_error if (operation && operation !~ /add|remove/) || (!mail_to_forward && operation)

  mail = find(local_part)

  begin
    case operation
    when 'add'
      mail.forward_to mail_to_forward
    when 'remove'
      mail.delete_forward_to mail_to_forward
    when nil
      mail.forward_list.each { |m| puts m }
    end
  rescue StandardError
    raise if options[:verbose]

    abort $ERROR_INFO
  end
end
keep(local_part, value = nil) click to toggle source
# File lib/sakura/cli/mail.rb, line 143
def keep(local_part, value = nil)
  preprocess

  self.class.handle_argument_error if value && value !~ /enable|disable/

  mail = find(local_part)

  begin
    case value
    when 'enable'
      mail.keep = true
    when 'disable'
      mail.keep = false
    when nil
      puts mail.keep
    end
  rescue StandardError
    raise if options[:verbose]

    abort $ERROR_INFO
  end
end
list() click to toggle source
# File lib/sakura/cli/mail.rb, line 14
def list
  preprocess

  addrs = MailAddress.all

  puts "# domain: #{Client.current_session.domain}"
  puts MailAddress.header
  addrs.each { |addr| puts addr }
end
password(local_part, password = nil) click to toggle source
# File lib/sakura/cli/mail.rb, line 76
def password(local_part, password = nil)
  preprocess

  password ||= ask_password
  mail = find(local_part)

  begin
    mail.password = password
  rescue StandardError
    raise if options[:verbose]

    abort $ERROR_INFO
  end
end
quota(local_part, value = nil) click to toggle source
# File lib/sakura/cli/mail.rb, line 56
def quota(local_part, value = nil)
  preprocess

  mail = find(local_part)

  begin
    if value
      mail.quota = value
    else
      puts mail.quota
    end
  rescue StandardError
    raise if options[:verbose]

    abort $ERROR_INFO
  end
end
scan(local_part, value = nil) click to toggle source
# File lib/sakura/cli/mail.rb, line 93
def scan(local_part, value = nil)
  preprocess

  self.class.handle_argument_error if value && value !~ /enable|disable/

  mail = find(local_part)

  begin
    case value
    when 'enable'
      mail.virus_scan = true
    when 'disable'
      mail.virus_scan = false
    when nil
      puts mail.virus_scan
    end
  rescue StandardError
    raise if options[:verbose]

    abort $ERROR_INFO
  end
end
show(local_part) click to toggle source
# File lib/sakura/cli/mail.rb, line 191
def show(local_part)
  preprocess

  puts find(local_part).detail
end

Private Instance Methods

abort(message) click to toggle source
Calls superclass method
# File lib/sakura/cli/mail.rb, line 225
def abort(message)
  super("\nERROR: #{message}")
end
ask_password() click to toggle source
# File lib/sakura/cli/mail.rb, line 215
def ask_password
  password = ask('password?', echo: false)
  puts
  confirm = ask('password(confirm)?', echo: false)
  puts
  abort "password doesn't match" unless password == confirm

  password
end
find(local_part) click to toggle source
# File lib/sakura/cli/mail.rb, line 203
def find(local_part)
  begin
    mail = MailAddress.find(local_part)
  rescue Capybara::ElementNotFound
    raise if options[:verbose]

    abort %(No mail address: "#{local_part}")
  end

  mail
end
preprocess() click to toggle source
# File lib/sakura/cli/mail.rb, line 199
def preprocess
  Client.verbose = true if options[:verbose]
end