Class: PostfixAdmin::CLI
- Inherits:
-
Object
- Object
- PostfixAdmin::CLI
- Defined in:
- lib/postfix_admin/cli.rb
Constant Summary collapse
- MIN_NUM_PASSWORD_CHARACTER =
5
Class Method Summary collapse
Instance Method Summary collapse
- #add_account(address, password, scheme = nil, name = nil) ⇒ Object
- #add_admin(user_name, password, super_admin = false, scheme = nil) ⇒ Object
- #add_admin_domain(user_name, domain_name) ⇒ Object
- #add_alias(address, goto) ⇒ Object
- #add_domain(domain_name) ⇒ Object
- #change_account_password(user_name, password) ⇒ Object
- #change_admin_password(user_name, password) ⇒ Object
- #db_setup ⇒ Object
- #delete_account(address) ⇒ Object
- #delete_admin(user_name) ⇒ Object
- #delete_admin_domain(user_name, domain_name) ⇒ Object
- #delete_alias(address) ⇒ Object
- #delete_domain(domain_name) ⇒ Object
- #dump ⇒ Object
- #edit_account(address, options) ⇒ Object
- #edit_admin(admin_name, options) ⇒ Object
- #edit_alias(address, options) ⇒ Object
- #edit_domain(domain_name, options) ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #log ⇒ Object
- #setup_domain(domain_name, password) ⇒ Object
- #show(name) ⇒ Object
- #show_account_details(user_name) ⇒ Object
- #show_address(domain_name) ⇒ Object
- #show_admin(domain_name = nil) ⇒ Object
- #show_admin_details(name) ⇒ Object
- #show_admin_domain(user_name) ⇒ Object
- #show_alias(domain_name) ⇒ Object
- #show_alias_details(name) ⇒ Object
- #show_domain ⇒ Object
- #show_summary(domain_name = nil) ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
10 11 12 13 |
# File 'lib/postfix_admin/cli.rb', line 10 def initialize @config = load_config @base = Base.new(@config) end |
Class Method Details
.config_file ⇒ Object
15 16 17 |
# File 'lib/postfix_admin/cli.rb', line 15 def self.config_file @config_file end |
.config_file=(value) ⇒ Object
19 20 21 |
# File 'lib/postfix_admin/cli.rb', line 19 def self.config_file=(value) @config_file = value end |
Instance Method Details
#add_account(address, password, scheme = nil, name = nil) ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/postfix_admin/cli.rb', line 270 def add_account(address, password, scheme = nil, name = nil) validate_password(password) @base.add_account(address, hashed_password(password, scheme), name) puts_registered(address, "an account") show_account_details(address) end |
#add_admin(user_name, password, super_admin = false, scheme = nil) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/postfix_admin/cli.rb', line 248 def add_admin(user_name, password, super_admin = false, scheme = nil) validate_password(password) @base.add_admin(user_name, hashed_password(password, scheme)) if super_admin Admin.find(user_name).super_admin = true puts_registered(user_name, "a super admin") else puts_registered(user_name, "an admin") end end |
#add_admin_domain(user_name, domain_name) ⇒ Object
260 261 262 263 |
# File 'lib/postfix_admin/cli.rb', line 260 def add_admin_domain(user_name, domain_name) @base.add_admin_domain(user_name, domain_name) puts_registered(domain_name, "a domain of #{user_name}") end |
#add_alias(address, goto) ⇒ Object
278 279 280 281 |
# File 'lib/postfix_admin/cli.rb', line 278 def add_alias(address, goto) @base.add_alias(address, goto) puts_registered("#{address}: #{goto}", "an alias") end |
#add_domain(domain_name) ⇒ Object
141 142 143 144 |
# File 'lib/postfix_admin/cli.rb', line 141 def add_domain(domain_name) @base.add_domain(domain_name) puts_registered(domain_name, "a domain") end |
#change_account_password(user_name, password) ⇒ Object
150 151 152 |
# File 'lib/postfix_admin/cli.rb', line 150 def change_account_password(user_name, password) change_password(Mailbox, user_name, password) end |
#change_admin_password(user_name, password) ⇒ Object
146 147 148 |
# File 'lib/postfix_admin/cli.rb', line 146 def change_admin_password(user_name, password) change_password(Admin, user_name, password) end |
#db_setup ⇒ Object
23 24 25 |
# File 'lib/postfix_admin/cli.rb', line 23 def db_setup @base.db_setup end |
#delete_account(address) ⇒ Object
322 323 324 325 |
# File 'lib/postfix_admin/cli.rb', line 322 def delete_account(address) @base.delete_account(address) puts_deleted(address) end |
#delete_admin(user_name) ⇒ Object
317 318 319 320 |
# File 'lib/postfix_admin/cli.rb', line 317 def delete_admin(user_name) @base.delete_admin(user_name) puts_deleted(user_name) end |
#delete_admin_domain(user_name, domain_name) ⇒ Object
265 266 267 268 |
# File 'lib/postfix_admin/cli.rb', line 265 def delete_admin_domain(user_name, domain_name) @base.delete_admin_domain(user_name, domain_name) puts "#{domain_name} was successfully deleted from #{user_name}" end |
#delete_alias(address) ⇒ Object
312 313 314 315 |
# File 'lib/postfix_admin/cli.rb', line 312 def delete_alias(address) @base.delete_alias(address) puts_deleted(address) end |
#delete_domain(domain_name) ⇒ Object
182 183 184 185 |
# File 'lib/postfix_admin/cli.rb', line 182 def delete_domain(domain_name) @base.delete_domain(domain_name) puts_deleted(domain_name) end |
#dump ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/postfix_admin/cli.rb', line 334 def dump puts "Admins" puts "User Name,Password,Super Admin,Active" Admin.all.each do |a| puts [a.username, %Q!"#{a.password}"!, a.super_admin?, a.active].join(',') end puts puts "Domains" puts "Domain Name,Max Quota,Active" Domain.without_all.each do |d| puts [d.domain, d.maxquota, d.active].join(',') end puts puts "Mailboxes" puts "User Name,Name,Password,Quota,Maildir,Active" Mailbox.all.each do |m| puts [m.username, %Q!"#{m.name}"!, %Q!"#{m.password}"!, m.quota, %Q!"#{m.maildir}"!, m.active].join(',') end puts puts "Aliases" puts "Address,Go to,Active" Alias.all.select { |a| !a.mailbox? }.each do |a| puts [a.address, %Q!"#{a.goto}"!, a.active].join(',') end puts puts "Forwards" puts "Address,Go to,Active" Alias.all.select { |a| a.mailbox? && a.goto != a.address }.each do |a| puts [a.address, %Q!"#{a.goto}"!, a.active].join(',') end end |
#edit_account(address, options) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/postfix_admin/cli.rb', line 283 def edit_account(address, ) mailbox_check(address) mailbox = Mailbox.find(address) mailbox.name = [:name] if [:name] mailbox.quota = [:quota] * KB_TO_MB if [:quota] mailbox.active = [:active] unless [:active].nil? mailbox.save! if [:goto] mail_alias = Alias.find(address) mail_alias.goto = [:goto] mail_alias.save! end puts "Successfully updated #{address}" show_account_details(address) end |
#edit_admin(admin_name, options) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/postfix_admin/cli.rb', line 154 def edit_admin(admin_name, ) admin_check(admin_name) admin = Admin.find(admin_name) unless [:super].nil? admin.super_admin = [:super] end admin.active = [:active] unless [:active].nil? admin.save! puts "Successfully updated #{admin_name}" show_admin_details(admin_name) end |
#edit_alias(address, options) ⇒ Object
301 302 303 304 305 306 307 308 309 310 |
# File 'lib/postfix_admin/cli.rb', line 301 def edit_alias(address, ) alias_check(address) mail_alias = Alias.find(address) mail_alias.goto = [:goto] if [:goto] mail_alias.active = [:active] unless [:active].nil? mail_alias.save or raise "Could not save Alias" puts "Successfully updated #{address}" show_alias_details(address) end |
#edit_domain(domain_name, options) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/postfix_admin/cli.rb', line 169 def edit_domain(domain_name, ) domain_check(domain_name) domain = Domain.find(domain_name) domain.aliases = [:aliases] if [:aliases] domain.mailboxes = [:mailboxes] if [:mailboxes] domain.maxquota = [:maxquota] if [:maxquota] domain.active = [:active] unless [:active].nil? domain.save! puts "Successfully updated #{domain_name}" show_summary(domain_name) end |
#log ⇒ Object
327 328 329 330 331 332 |
# File 'lib/postfix_admin/cli.rb', line 327 def log Log.all.each do |l| time = l..strftime("%Y-%m-%d %X %Z") puts "#{time} #{l.username} #{l.domain} #{l.action} #{l.data}" end end |
#setup_domain(domain_name, password) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/postfix_admin/cli.rb', line 80 def setup_domain(domain_name, password) admin = "admin@#{domain_name}" add_domain(domain_name) add_admin(admin, password) add_admin_domain(admin, domain_name) end |
#show(name) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/postfix_admin/cli.rb', line 27 def show(name) name = name.downcase if name if name =~ /@/ if Admin.exists?(name) show_admin_details(name) end if Mailbox.exists?(name) show_account_details(name) elsif Alias.exists?(name) show_alias_details(name) end return end show_summary(name) if name show_admin(name) show_address(name) show_alias(name) else show_domain show_admin end end |
#show_account_details(user_name) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/postfix_admin/cli.rb', line 87 def show_account_details(user_name) account_check(user_name) mailbox = Mailbox.find(user_name) mail_alias = Alias.find(user_name) report("Mailbox") do puts "Address : %s" % mailbox.username puts "Name : %s" % mailbox.name puts "Password : %s" % mailbox.password puts "Quota : %d MB" % max_str(mailbox.quota / KB_TO_MB) puts "Go to : %s" % mail_alias.goto puts "Active : %s" % mailbox.active_str end end |
#show_address(domain_name) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/postfix_admin/cli.rb', line 203 def show_address(domain_name) domain_check(domain_name) mailboxes = Domain.find(domain_name).rel_mailboxes index = " No. Email Name Quota (MB) Active Maildir" report("Addresses", index) do if mailboxes.empty? puts " No addresses" next end mailboxes.each_with_index do |m, i| quota = m.quota.to_f/ KB_TO_MB.to_f puts "%4d %-30s %-20s %10s %-3s %s" % [i+1, m.username, m.name, max_str(quota.to_i), m.active_str, m.maildir] end end end |
#show_admin(domain_name = nil) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/postfix_admin/cli.rb', line 187 def show_admin(domain_name = nil) admins = domain_name ? Admin.select { |a| a.rel_domains.exists?(domain_name) } : Admin.all index = " No. Admin Domains Active" report("Admins", index) do if admins.empty? puts " No admins" next end admins.each_with_index do |a, i| domains = a.super_admin? ? 'Super admin' : a.rel_domains.count puts "%4d %-40s %11s %-3s" % [i+1, a.username, domains, a.active_str] end end end |
#show_admin_details(name) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/postfix_admin/cli.rb', line 102 def show_admin_details(name) admin_check(name) admin = Admin.find(name) report("Admin") do puts "Name : %s" % admin.username puts "Password : %s" % admin.password puts "Domains : %s" % (admin.super_admin? ? "ALL" : admin.rel_domains.count) puts "Role : %s" % (admin.super_admin? ? "Super admin" : "Admin") puts "Active : %s" % admin.active_str end end |
#show_admin_domain(user_name) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/postfix_admin/cli.rb', line 234 def show_admin_domain(user_name) admin = Admin.find(user_name) if admin.rel_domains.empty? puts "\nNo domain in database" return end report("Domains (#{user_name})", " No. Domain") do admin.rel_domains.each_with_index do |d, i| puts "%4d %-30s" % [i + 1, d.domain] end end end |
#show_alias(domain_name) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/postfix_admin/cli.rb', line 221 def show_alias(domain_name) domain_check(domain_name) forwards, aliases = Domain.find(domain_name).rel_aliases.partition { |a| a.mailbox? } forwards.delete_if do |f| f.address == f.goto end show_alias_base("Forwards", forwards) show_alias_base("Aliases", aliases) end |
#show_alias_details(name) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/postfix_admin/cli.rb', line 115 def show_alias_details(name) alias_check(name) mail_alias = Alias.find(name) report("Alias") do puts "Address : %s" % mail_alias.address puts "Go to : %s" % mail_alias.goto puts "Active : %s" % mail_alias.active_str end end |
#show_domain ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/postfix_admin/cli.rb', line 125 def show_domain index = " No. Domain Aliases Mailboxes Quota (MB) Active" report('Domains', index) do if Domain.without_all.empty? puts " No domains" next end Domain.without_all.each_with_index do |d, i| puts "%4d %-30s %3d /%3s %3d /%3s %10d %-3s" % [i+1, d.domain, d.pure_aliases.count, max_str(d.aliases), d.rel_mailboxes.count, max_str(d.mailboxes), d.maxquota, d.active_str] end end end |
#show_summary(domain_name = nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/postfix_admin/cli.rb', line 56 def show_summary(domain_name = nil) title = "Summary" if domain_name domain_name = domain_name.downcase domain_check(domain_name) title = "Summary of #{domain_name}" end report(title) do if domain_name domain = Domain.find(domain_name) puts "Mailboxes : %4d / %4s" % [domain.rel_mailboxes.count, max_str(domain.mailboxes)] puts "Aliases : %4d / %4s" % [domain.pure_aliases.count, max_str(domain.aliases)] puts "Max Quota : %4d MB" % domain.maxquota puts "Active : %3s" % domain.active_str else puts "Domains : %4d" % Domain.without_all.count puts "Admins : %4d" % Admin.count puts "Mailboxes : %4d" % Mailbox.count puts "Aliases : %4d" % Alias.pure.count end end end |