class RIMS::Cmd::Config

Constants

IMAP_AUTH_TYPE_LIST
IMAP_CONNECT_OPTION_LIST
IMAP_MAILBOX_OPTION_LIST
IMAP_STORE_FLAG_OPTION_LIST
MAIL_DATE_OPTION_LIST
MAIL_DATE_PLACE_LIST
POST_MAIL_CONNECT_OPTION_LIST
VERBOSE_OPTION_LIST

Public Class Methods

make_imap_connect_option_list(imap_host: 'localhost', imap_port: 143, imap_ssl: false, auth_type: 'login', username: nil) click to toggle source
# File lib/rims/cmd.rb, line 859
def self.make_imap_connect_option_list(imap_host: 'localhost', imap_port: 143, imap_ssl: false, auth_type: 'login', username: nil)
  [ [ :imap_host,  imap_host, '-n', '--host=HOSTNAME',                  "Hostname or IP address to connect IMAP server. default is `#{imap_host}'." ],
    [ :imap_port,  imap_port, '-o', '--port=PORT', Integer,             "Server port number or service name to connect IMAP server. default is #{imap_port}." ],
    [ :imap_ssl,   imap_ssl,  '-s', '--[no-]use-ssl',                   "Enable SSL/TLS connection. default is #{imap_ssl ? 'enabled' : 'disabled'}." ],
    [ :ca_cert,    nil,              '--ca-cert=PATH',                  "CA cert file or directory." ],
    [ :ssl_params, {},               '--ssl-params=JSON_DATA', JSON,    "SSLContext#set_params as parameters." ],
    [ :username,   username,  '-u', '--username=NAME',                  "Username to login IMAP server. " +
                                                                        (username ? "default is `#{username}'." : "required parameter to connect server.") ],
    [ :password,  nil,       '-w', '--password=PASS',                   "Password to login IMAP server. required parameter to connect server." ],
    [ :auth_type, auth_type, '--auth-type=METHOD', IMAP_AUTH_TYPE_LIST, "Choose authentication method type (#{IMAP_AUTH_TYPE_LIST.join(' ')}). " +
                                                                        "default is `#{auth_type}'." ]
  ]
end
new(options, option_list) click to toggle source
# File lib/rims/cmd.rb, line 927
def initialize(options, option_list)
  @options = options
  @option_list = option_list
  @conf = {}
  for key, value, *_option_description in option_list
    @conf[key] = value
  end
end
symbolize_string_key(collection) click to toggle source
# File lib/rims/cmd.rb, line 895
def self.symbolize_string_key(collection)
  case (collection)
  when Hash
    Hash[collection.map{|key, value|
           [ symbolize_string_key(key),
             case (value)
             when Hash, Array
               symbolize_string_key(value)
             else
               value
             end
           ]
         }]
  when Array
    collection.map{|value|
      case (value)
      when Hash, Array
        symbolize_string_key(value)
      else
        value
      end
    }
  else
    case (value = collection)
    when String
      value.to_sym
    else
      value
    end
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/rims/cmd.rb, line 936
def [](key)
  @conf[key]
end
help_option(add_banner: nil) click to toggle source
# File lib/rims/cmd.rb, line 950
def help_option(add_banner: nil)
  @options.banner += add_banner if add_banner
  @options.on('-h', '--help', 'Show this message.') do
    puts @options
    exit
  end

  self
end
imap_connect() { |imap| ... } click to toggle source
# File lib/rims/cmd.rb, line 1038
def imap_connect
  unless (@conf[:username] && @conf[:password]) then
    raise 'need for username and password.'
  end

  args = [ @conf[:imap_host] ]
  if (@conf[:imap_ssl]) then
    if (@conf[:ssl_params].empty?) then
      args << @conf[:imap_port]
      args << @conf[:imap_ssl]
      args << @conf[:ca_cert]
    else
      kw_args = {
        port: @conf[:imap_port],
        ssl: @conf[:ssl_params]
      }
      args << kw_args
    end
  else
    args << @conf[:imap_port]
  end

  imap = Net::IMAP.new(*args)
  begin
    if (@conf[:verbose]) then
      puts "server greeting: #{imap_res2str(imap.greeting)}"
      puts "server capability: #{imap.capability.join(' ')}"
    end

    case (@conf[:auth_type])
    when 'login'
      res = imap.login(@conf[:username], @conf[:password])
      puts "login: #{imap_res2str(res)}" if @conf[:verbose]
    when 'plain', 'cram-md5'
      res = imap.authenticate(@conf[:auth_type], @conf[:username], @conf[:password])
      puts "authenticate: #{imap_res2str(res)}" if @conf[:verbose]
    else
      raise "unknown authentication type: #{@conf[:auth_type]}"
    end

    yield(imap)
  ensure
    imap.logout
  end
end
imap_debug_option() click to toggle source
# File lib/rims/cmd.rb, line 1029
def imap_debug_option
  @options.on('--[no-]imap-debug',
              "Set the debug flag of Net::IMAP class. default is false.") do |v|
    Net::IMAP.debug = v
  end

  self
end
key_value_store_option() click to toggle source
# File lib/rims/cmd.rb, line 996
def key_value_store_option
  @conf[:key_value_store_type] = GDBM_KeyValueStore
  @options.on('--kvs-type=TYPE',
              "Choose key-value store type of mailbox database. default is `" +
              KeyValueStore::FactoryBuilder.plug_in_names[0] +
              "'."
             ) do |kvs_type|
    @conf[:key_value_store_type] = KeyValueStore::FactoryBuilder.get_plug_in(kvs_type)
  end

  @conf[:use_key_value_store_checksum] = true
  @options.on('--[no-]use-kvs-checksum', 'Enable/disable data checksum at key-value store. default is enabled.') do |use_checksum|
    @conf[:use_key_value_store_checksum] = use_checksum
  end
  @options.on('--[no-]use-kvs-cksum', 'Deplicated.') do |use_checksum|
    warn("warning: `--[no-]use-kvs-cksum' is deplicated option and should use `--[no-]use-kvs-checksum'.")
    @conf[:use_key_value_store_checksum] = use_checksum
  end

  self
end
load_config_option() click to toggle source
# File lib/rims/cmd.rb, line 972
def load_config_option
  @options.on('-f', '--config-yaml=CONFIG_FILE',
              String,
              "Load optional parameters from CONFIG_FILE.") do |path|
    config = YAML.load_file(path)
    symbolized_config = self.class.symbolize_string_key(config)
    @conf.update(symbolized_config)
  end

  self
end
look_for_date(message_text, path=nil) click to toggle source
# File lib/rims/cmd.rb, line 1101
def look_for_date(message_text, path=nil)
  case (@conf[:look_for_date])
  when :servertime
    nil
  when :localtime
    Time.now
  when :filetime
    if (path) then
      File.stat(path).mtime
    end
  when :mailheader
    RFC822::Message.new(message_text).date
  else
    raise "failed to look for date: #{place}"
  end
end
make_imap_store_flags() click to toggle source
# File lib/rims/cmd.rb, line 1084
def make_imap_store_flags
  store_flags = []
  [ [ :store_flag_answered, :Answered ],
    [ :store_flag_flagged, :Flagged ],
    [ :store_flag_deleted, :Deleted ],
    [ :store_flag_seen, :Seen ],
    [ :store_flag_draft, :Draft ]
  ].each do |key, flag|
    if (@conf[key]) then
      store_flags << flag
    end
  end
  puts "store flags: (#{store_flags.join(' ')})" if @conf[:verbose]

  store_flags
end
make_kvs_factory() click to toggle source
# File lib/rims/cmd.rb, line 1118
def make_kvs_factory
  builder = KeyValueStore::FactoryBuilder.new
  builder.open{|name| @conf[:key_value_store_type].open_with_conf(name, {}) }
  if (@conf[:use_key_value_store_checksum]) then
    builder.use(Checksum_KeyValueStore)
  end
  builder.factory
end
parse_options!(args, order: false) click to toggle source
# File lib/rims/cmd.rb, line 1018
def parse_options!(args, order: false)
  if (order) then
    @options.order!(args)
  else
    @options.parse!(args)
  end
  pp @conf if $DEBUG

  self
end
quiet_option(default_verbose: true) click to toggle source
# File lib/rims/cmd.rb, line 960
def quiet_option(default_verbose: true)
  @conf[:verbose] = default_verbose
  @options.on('-v', '--[no-]verbose', 'Enable verbose messages. default is verbose.') do |verbose|
    @conf[:verbose] = verbose
  end
  @options.on('-q', '--[no-]quiet', 'Disable verbose messages. default is verbose.') do |quiet|
    @conf[:verbose] = ! quiet
  end

  self
end
required_feature_option() click to toggle source
# File lib/rims/cmd.rb, line 984
def required_feature_option
  @options.on('-r', '--required-feature=FEATURE', String, 'Add required feature.') do |feature|
    require(feature)
  end
  @options.on('--load-library=LIBRARY', String, 'Deplicated.') do |library|
    warn("warning: `--load-library=LIBRARY' is deplicated option and should use `--required-feature=FEATURE'.")
    require(library)
  end

  self
end
setup_option_list() click to toggle source
# File lib/rims/cmd.rb, line 940
def setup_option_list
  @option_list.each do |key, value, *option_description|
    @options.on(*option_description) do |v|
      @conf[key] = v
    end
  end

  self
end

Private Instance Methods

imap_res2str(imap_response) click to toggle source
# File lib/rims/cmd.rb, line 847
def imap_res2str(imap_response)
  Cmd.imap_res2str(imap_response)
end