Class: MPW::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mpw/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ Config

Returns a new instance of Config.

Parameters:

  • config_file (String) (defaults to: nil)

    path of config file



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mpw/config.rb', line 39

def initialize(config_file = nil)
  @config_file = config_file
  @config_dir  =
    if /darwin/ =~ RUBY_PLATFORM
      "#{Dir.home}/Library/Preferences/mpw"
    elsif /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
      "#{Dir.home}/AppData/Local/mpw"
    else
      "#{Dir.home}/.config/mpw"
    end

  @config_file = "#{@config_dir}/mpw.cfg" if @config_file.to_s.empty?
end

Instance Attribute Details

#config_dirObject

Returns the value of attribute config_dir.



30
31
32
# File 'lib/mpw/config.rb', line 30

def config_dir
  @config_dir
end

#default_walletObject

Returns the value of attribute default_wallet.



31
32
33
# File 'lib/mpw/config.rb', line 31

def default_wallet
  @default_wallet
end

#error_msgObject

Returns the value of attribute error_msg.



26
27
28
# File 'lib/mpw/config.rb', line 26

def error_msg
  @error_msg
end

#gpg_exeObject

Returns the value of attribute gpg_exe.



34
35
36
# File 'lib/mpw/config.rb', line 34

def gpg_exe
  @gpg_exe
end

#gpg_keyObject

Returns the value of attribute gpg_key.



28
29
30
# File 'lib/mpw/config.rb', line 28

def gpg_key
  @gpg_key
end

#langObject

Returns the value of attribute lang.



29
30
31
# File 'lib/mpw/config.rb', line 29

def lang
  @lang
end

#passwordObject

Returns the value of attribute password.



35
36
37
# File 'lib/mpw/config.rb', line 35

def password
  @password
end

#pinmodeObject

Returns the value of attribute pinmode.



36
37
38
# File 'lib/mpw/config.rb', line 36

def pinmode
  @pinmode
end

#wallet_dirObject

Returns the value of attribute wallet_dir.



32
33
34
# File 'lib/mpw/config.rb', line 32

def wallet_dir
  @wallet_dir
end

#wallet_pathsObject

Returns the value of attribute wallet_paths.



33
34
35
# File 'lib/mpw/config.rb', line 33

def wallet_paths
  @wallet_paths
end

Instance Method Details

#check_gpg_key?Boolean

Check if private key exist

Returns:

  • (Boolean)

    true if the key exist, else false



150
151
152
153
154
155
156
157
# File 'lib/mpw/config.rb', line 150

def check_gpg_key?
  ctx = GPGME::Ctx.new
  ctx.each_key(@gpg_key, true) do
    return true
  end

  false
end

#load_configObject

Load the config file



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mpw/config.rb', line 130

def load_config
  config          = YAML.load_file(@config_file)
  @gpg_key        = config['gpg_key']
  @lang           = config['lang']
  @wallet_dir     = config['wallet_dir']
  @wallet_paths   = config['wallet_paths'] || {}
  @default_wallet = config['default_wallet']
  @gpg_exe        = config['gpg_exe']
  @password       = config['password'] || {}
  @pinmode        = config['pinmode'] || false

  raise if @gpg_key.empty? || @wallet_dir.empty?

  I18n.locale = @lang.to_sym
rescue => e
  raise "#{I18n.t('error.config.load')}\n#{e}"
end

#set_wallet_path(path, wallet) ⇒ Object

Change the path of one wallet

Parameters:

  • path (String)

    new directory path

  • wallet (String)

    wallet name



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/mpw/config.rb', line 162

def set_wallet_path(path, wallet)
  path = @wallet_dir if path == 'default'
  path = File.absolute_path(path)

  return if path == @wallet_dir && File.exist?("#{@wallet_dir}/#{wallet}.mpw")
  return if path == @wallet_paths[wallet]

  old_wallet_file =
    if @wallet_paths.key?(wallet)
      "#{@wallet_paths[wallet]}/#{wallet}.mpw"
    else
      "#{@wallet_dir}/#{wallet}.mpw"
    end

  FileUtils.mkdir_p(path) unless Dir.exist?(path)
  FileUtils.mv(old_wallet_file, "#{path}/#{wallet}.mpw") if File.exist?(old_wallet_file)

  if path == @wallet_dir
    @wallet_paths.delete(wallet)
  else
    @wallet_paths[wallet] = path
  end

  setup
end

#setup(**options) ⇒ Object

Create a new config file

Parameters:

  • options (Hash)

    the value to set the config file



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mpw/config.rb', line 55

def setup(**options)
  gpg_key        = options[:gpg_key]        || @gpg_key
  lang           = options[:lang]           || @lang
  wallet_dir     = options[:wallet_dir]     || @wallet_dir
  default_wallet = options[:default_wallet] || @default_wallet
  gpg_exe        = options[:gpg_exe]        || @gpg_exe
  pinmode        = options.key?(:pinmode) ? options[:pinmode] : @pinmode
  password       = {
    numeric: true,
    alpha:   true,
    special: false,
    length:  16
  }

  %w[numeric special alpha length].each do |k|
    if options.key?("pwd_#{k}".to_sym)
      password[k.to_sym] = options["pwd_#{k}".to_sym]
    elsif !@password.nil? && @password.key?(k.to_sym)
      password[k.to_sym] = @password[k.to_sym]
    end
  end

  unless gpg_key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
    raise I18n.t('error.config.key_bad_format')
  end

  wallet_dir = "#{@config_dir}/wallets" if wallet_dir.to_s.empty?
  config     = { 'gpg_key'        => gpg_key,
                 'lang'           => lang,
                 'wallet_dir'     => wallet_dir,
                 'default_wallet' => default_wallet,
                 'gpg_exe'        => gpg_exe,
                 'password'       => password,
                 'pinmode'        => pinmode,
                 'wallet_paths'   => @wallet_paths }

  FileUtils.mkdir_p(@config_dir, mode: 0700)
  FileUtils.mkdir_p(wallet_dir,  mode: 0700)

  File.open(@config_file, 'w') do |file|
    file << config.to_yaml
  end
rescue => e
  raise "#{I18n.t('error.config.write')}\n#{e}"
end

#setup_gpg_key(password, name, length = 4096, expire = 0) ⇒ Object

Setup a new gpg key

Parameters:

  • password (String)

    gpg key password

  • name (String)

    the name of user

  • length (Integer) (defaults to: 4096)

    length of the gpg key

  • expire (Integer) (defaults to: 0)

    time of expire to gpg key



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mpw/config.rb', line 106

def setup_gpg_key(password, name, length = 4096, expire = 0)
  raise I18n.t('error.config.genkey_gpg.name') if name.to_s.empty?
  raise I18n.t('error.config.genkey_gpg.password') if password.to_s.empty?

  param = ''
  param << '<GnupgKeyParms format="internal">' + "\n"
  param << "Key-Type: RSA\n"
  param << "Key-Length: #{length}\n"
  param << "Subkey-Type: ELG-E\n"
  param << "Subkey-Length: #{length}\n"
  param << "Name-Real: #{name}\n"
  param << "Name-Comment: #{name}\n"
  param << "Name-Email: #{@gpg_key}\n"
  param << "Expire-Date: #{expire}\n"
  param << "Passphrase: #{password}\n"
  param << "</GnupgKeyParms>\n"

  ctx = GPGME::Ctx.new
  ctx.genkey(param, nil, nil)
rescue => e
  raise "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}"
end