Class: MPW::Config
- Inherits:
-
Object
- Object
- MPW::Config
- Defined in:
- lib/mpw/config.rb
Instance Attribute Summary collapse
-
#dir_config ⇒ Object
Returns the value of attribute dir_config.
-
#error_msg ⇒ Object
Returns the value of attribute error_msg.
-
#file_gpg ⇒ Object
Returns the value of attribute file_gpg.
-
#key ⇒ Object
Returns the value of attribute key.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#last_sync ⇒ Object
Returns the value of attribute last_sync.
-
#last_update ⇒ Object
Returns the value of attribute last_update.
-
#share_keys ⇒ Object
Returns the value of attribute share_keys.
-
#sync_host ⇒ Object
Returns the value of attribute sync_host.
-
#sync_path ⇒ Object
Returns the value of attribute sync_path.
-
#sync_port ⇒ Object
Returns the value of attribute sync_port.
-
#sync_pwd ⇒ Object
Returns the value of attribute sync_pwd.
-
#sync_type ⇒ Object
Returns the value of attribute sync_type.
-
#sync_user ⇒ Object
Returns the value of attribute sync_user.
Instance Method Summary collapse
-
#check_gpg_key? ⇒ Boolean
Check if private key exist @rtrn: true if the key exist, else false.
-
#check_public_gpg_key(share_keys = @share_keys) ⇒ Object
Check if private key exist @args: share_keys -> string with all public keys @rtrn: true if the key exist, else false.
-
#checkconfig ⇒ Object
Check the config file @rtrn: true if the config file is correct.
-
#initialize(file_config = nil) ⇒ Config
constructor
Constructor @args: file_config -> the specify config file.
-
#set_last_sync ⇒ Object
Set the last update when there is a sync @rtrn: true is the file has been updated.
-
#setup(key, share_keys, lang, file_gpg, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path) ⇒ Object
Create a new config file @args: key -> the gpg key to encrypt share_keys -> multiple keys to share the password with other people lang -> the software language file_gpg -> the file who is encrypted sync_type -> the type to synchronization sync_host -> the server host for synchronization sync_port -> the server port for synchronization sync_user -> the user for synchronization sync_pwd -> the password for synchronization sync_suffix -> the suffix file (optionnal) @rtrn: true if le config file is create.
-
#setup_gpg_key(password, name, length = 2048, 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(file_config = nil) ⇒ Config
Constructor @args: file_config -> the specify config file
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mpw/config.rb', line 31 def initialize(file_config=nil) @error_msg = nil if /darwin/ =~ RUBY_PLATFORM @dir_config = "#{Dir.home}/Library/Preferences/mpw" elsif /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM @dir_config = "#{Dir.home}/AppData/Local/mpw" else @dir_config = "#{Dir.home}/.config/mpw" end @file_config = "#{@dir_config}/conf/default.cfg" if not file_config.nil? and not file_config.empty? @file_config = file_config end end |
Instance Attribute Details
#dir_config ⇒ Object
Returns the value of attribute dir_config.
27 28 29 |
# File 'lib/mpw/config.rb', line 27 def dir_config @dir_config end |
#error_msg ⇒ Object
Returns the value of attribute error_msg.
13 14 15 |
# File 'lib/mpw/config.rb', line 13 def error_msg @error_msg end |
#file_gpg ⇒ Object
Returns the value of attribute file_gpg.
18 19 20 |
# File 'lib/mpw/config.rb', line 18 def file_gpg @file_gpg end |
#key ⇒ Object
Returns the value of attribute key.
15 16 17 |
# File 'lib/mpw/config.rb', line 15 def key @key end |
#lang ⇒ Object
Returns the value of attribute lang.
17 18 19 |
# File 'lib/mpw/config.rb', line 17 def lang @lang end |
#last_sync ⇒ Object
Returns the value of attribute last_sync.
26 27 28 |
# File 'lib/mpw/config.rb', line 26 def last_sync @last_sync end |
#last_update ⇒ Object
Returns the value of attribute last_update.
19 20 21 |
# File 'lib/mpw/config.rb', line 19 def last_update @last_update end |
#share_keys ⇒ Object
Returns the value of attribute share_keys.
16 17 18 |
# File 'lib/mpw/config.rb', line 16 def share_keys @share_keys end |
#sync_host ⇒ Object
Returns the value of attribute sync_host.
21 22 23 |
# File 'lib/mpw/config.rb', line 21 def sync_host @sync_host end |
#sync_path ⇒ Object
Returns the value of attribute sync_path.
25 26 27 |
# File 'lib/mpw/config.rb', line 25 def sync_path @sync_path end |
#sync_port ⇒ Object
Returns the value of attribute sync_port.
22 23 24 |
# File 'lib/mpw/config.rb', line 22 def sync_port @sync_port end |
#sync_pwd ⇒ Object
Returns the value of attribute sync_pwd.
24 25 26 |
# File 'lib/mpw/config.rb', line 24 def sync_pwd @sync_pwd end |
#sync_type ⇒ Object
Returns the value of attribute sync_type.
20 21 22 |
# File 'lib/mpw/config.rb', line 20 def sync_type @sync_type end |
#sync_user ⇒ Object
Returns the value of attribute sync_user.
23 24 25 |
# File 'lib/mpw/config.rb', line 23 def sync_user @sync_user end |
Instance Method Details
#check_gpg_key? ⇒ Boolean
Check if private key exist @rtrn: true if the key exist, else false
168 169 170 171 172 173 174 175 |
# File 'lib/mpw/config.rb', line 168 def check_gpg_key? ctx = GPGME::Ctx.new ctx.each_key(@key, true) do return true end return false end |
#check_public_gpg_key(share_keys = @share_keys) ⇒ Object
Check if private key exist @args: share_keys -> string with all public keys @rtrn: true if the key exist, else false
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/mpw/config.rb', line 180 def check_public_gpg_key(share_keys = @share_keys) ctx = GPGME::Ctx.new share_keys = share_keys.nil? ? '' : share_keys if not share_keys.empty? share_keys.split.each do |k| if not k =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ @error_msg = I18n.t('error.config.key_bad_format') return false end ctx.each_key(key, false) do next end @error_msg = I18n.t('error.config.no_key_public', key: k) return false end end return true end |
#checkconfig ⇒ Object
Check the config file @rtrn: true if the config file is correct
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mpw/config.rb', line 140 def checkconfig config = YAML::load_file(@file_config) @key = config['config']['key'] @share_keys = config['config']['share_keys'] @lang = config['config']['lang'] @file_gpg = config['config']['file_gpg'] @sync_type = config['config']['sync_type'] @sync_host = config['config']['sync_host'] @sync_port = config['config']['sync_port'] @sync_user = config['config']['sync_user'] @sync_pwd = config['config']['sync_pwd'] @sync_path = config['config']['sync_path'] @last_sync = config['config']['last_sync'].to_i if @key.empty? or @file_gpg.empty? @error_msg = I18n.t('error.config.check') return false end I18n.locale = @lang.to_sym return true rescue Exception => e @error_msg = "#{I18n.t('error.config.check')}\n#{e}" return false end |
#set_last_sync ⇒ Object
Set the last update when there is a sync @rtrn: true is the file has been updated
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/mpw/config.rb', line 205 def set_last_sync config = {'config' => {'key' => @key, 'share_keys' => @share_keys, 'lang' => @lang, 'file_gpg' => @file_gpg, 'sync_type' => @sync_type, 'sync_host' => @sync_host, 'sync_port' => @sync_port, 'sync_user' => @sync_user, 'sync_pwd' => @sync_pwd, 'sync_path' => @sync_path, 'last_sync' => Time.now.to_i } } File.open(@file_config, 'w') do |file| file << config.to_yaml end return true rescue Exception => e @error_msg = "#{I18n.t('error.config.write')}\n#{e}" return false end |
#setup(key, share_keys, lang, file_gpg, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path) ⇒ Object
Create a new config file @args: key -> the gpg key to encrypt
share_keys -> multiple keys to share the password with other people
lang -> the software language
file_gpg -> the file who is encrypted
sync_type -> the type to synchronization
sync_host -> the server host for synchronization
sync_port -> the server port for synchronization
sync_user -> the user for synchronization
sync_pwd -> the password for synchronization
sync_suffix -> the suffix file (optionnal)
@rtrn: true if le config file is create
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 60 def setup(key, share_keys, lang, file_gpg, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path) if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ @error_msg = I18n.t('error.config.key_bad_format') return false end if not check_public_gpg_key(share_keys) return false end if file_gpg.empty? file_gpg = "#{@dir_config}/db/default.gpg" end config = {'config' => {'key' => key, 'share_keys' => share_keys, 'lang' => lang, 'file_gpg' => file_gpg, 'sync_type' => sync_type, 'sync_host' => sync_host, 'sync_port' => sync_port, 'sync_user' => sync_user, 'sync_pwd' => sync_pwd, 'sync_path' => sync_path, 'last_sync' => 0 } } Dir.mkdir("#{@config_dir}/conf", 700) Dir.mkdir("#{@config_dir}/db", 700) File.open(@file_config, 'w') do |file| file << config.to_yaml end return true rescue Exception => e @error_msg = "#{I18n.t('error.config.write')}\n#{e}" return false end |
#setup_gpg_key(password, name, length = 2048, 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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mpw/config.rb', line 107 def setup_gpg_key(password, name, length = 2048, expire = 0) if name.nil? or name.empty? @error_msg = "#{I18n.t('error.config.genkey_gpg.name')}" return false elsif password.nil? or password.empty? @error_msg = "#{I18n.t('error.config.genkey_gpg.password')}" return false 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) return true rescue Exception => e @error_msg = "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}" return false end |