Class: MPW::Config
- Inherits:
-
Object
- Object
- MPW::Config
- Defined in:
- lib/mpw/config.rb
Instance Attribute Summary collapse
-
#config_dir ⇒ Object
Returns the value of attribute config_dir.
-
#error_msg ⇒ Object
Returns the value of attribute error_msg.
-
#gpg_exe ⇒ Object
Returns the value of attribute gpg_exe.
-
#key ⇒ Object
Returns the value of attribute key.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#wallet_dir ⇒ Object
Returns the value of attribute wallet_dir.
Instance Method Summary collapse
-
#check_gpg_key? ⇒ Boolean
Check if private key exist @rtrn: true if the key exist, else false.
-
#initialize(config_file = nil) ⇒ Config
constructor
Constructor @args: config_file -> the specify config file.
-
#is_valid? ⇒ Boolean
Check the config file @rtrn: true if the config file is correct.
-
#setup(key, lang, wallet_dir, gpg_exe) ⇒ Object
Create a new config file @args: key -> the gpg key to encrypt lang -> the software language wallet_dir -> the directory where are the wallets password gpg_exe -> the path of gpg executable @rtrn: true if le config file is create.
-
#setup_gpg_key(password, name, length = 4096, expire = 0) ⇒ Object
Setup a new gpg key @args: password -> the GPG key password name -> the name of user length -> length of the GPG key expire -> the time of expire to GPG key @rtrn: true if the GPG key is create, else false.
Constructor Details
#initialize(config_file = nil) ⇒ Config
Constructor @args: config_file -> the specify config file
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mpw/config.rb', line 37 def initialize(config_file=nil) @config_file = config_file if /darwin/ =~ RUBY_PLATFORM @config_dir = "#{Dir.home}/Library/Preferences/mpw" elsif /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM @config_dir = "#{Dir.home}/AppData/Local/mpw" else @config_dir = "#{Dir.home}/.config/mpw" end if @config_file.nil? or @config_file.empty? @config_file = "#{@config_dir}/mpw.cfg" end end |
Instance Attribute Details
#config_dir ⇒ Object
Returns the value of attribute config_dir.
31 32 33 |
# File 'lib/mpw/config.rb', line 31 def config_dir @config_dir end |
#error_msg ⇒ Object
Returns the value of attribute error_msg.
27 28 29 |
# File 'lib/mpw/config.rb', line 27 def error_msg @error_msg end |
#gpg_exe ⇒ Object
Returns the value of attribute gpg_exe.
33 34 35 |
# File 'lib/mpw/config.rb', line 33 def gpg_exe @gpg_exe end |
#key ⇒ Object
Returns the value of attribute key.
29 30 31 |
# File 'lib/mpw/config.rb', line 29 def key @key end |
#lang ⇒ Object
Returns the value of attribute lang.
30 31 32 |
# File 'lib/mpw/config.rb', line 30 def lang @lang end |
#wallet_dir ⇒ Object
Returns the value of attribute wallet_dir.
32 33 34 |
# File 'lib/mpw/config.rb', line 32 def wallet_dir @wallet_dir end |
Instance Method Details
#check_gpg_key? ⇒ Boolean
Check if private key exist @rtrn: true if the key exist, else false
137 138 139 140 141 142 143 144 |
# File 'lib/mpw/config.rb', line 137 def check_gpg_key? ctx = GPGME::Ctx.new ctx.each_key(@key, true) do return true end return false end |
#is_valid? ⇒ Boolean
Check the config file @rtrn: true if the config file is correct
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/mpw/config.rb', line 119 def is_valid? config = YAML::load_file(@config_file) @key = config['config']['key'] @lang = config['config']['lang'] @wallet_dir = config['config']['wallet_dir'] @gpg_exe = config['config']['gpg_exe'] raise if @key.empty? or @wallet_dir.empty? I18n.locale = @lang.to_sym return true rescue return false end |
#setup(key, lang, wallet_dir, gpg_exe) ⇒ Object
Create a new config file @args: key -> the gpg key to encrypt
lang -> the software language
wallet_dir -> the directory where are the wallets password
gpg_exe -> the path of gpg executable
@rtrn: true if le config file is create
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 |
# File 'lib/mpw/config.rb', line 59 def setup(key, lang, wallet_dir, gpg_exe) if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ raise I18n.t('error.config.key_bad_format') end if wallet_dir.empty? wallet_dir = "#{@config_dir}/wallets" end config = {'config' => {'key' => key, 'lang' => lang, 'wallet_dir' => wallet_dir, 'gpg_exe' => gpg_exe, } } FileUtils.mkdir_p(wallet_dir, mode: 0700) File.open(@config_file, 'w') do |file| file << config.to_yaml end rescue Exception => 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 @args: password -> the GPG key password
name -> the name of user
length -> length of the GPG key
expire -> the time of expire to GPG key
@rtrn: true if the GPG key is create, else false
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mpw/config.rb', line 91 def setup_gpg_key(password, name, length = 4096, expire = 0) if name.nil? or name.empty? raise "#{I18n.t('error.config.genkey_gpg.name')}" elsif password.nil? or password.empty? raise "#{I18n.t('error.config.genkey_gpg.password')}" end param = '' param << '<GnupgKeyParms format="internal">' + "\n" param << "Key-Type: DSA\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: #{@key}\n" param << "Expire-Date: #{expire}\n" param << "Passphrase: #{password}\n" param << "</GnupgKeyParms>\n" ctx = GPGME::Ctx.new ctx.genkey(param, nil, nil) rescue Exception => e raise "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}" end |