Class: PostfixAdmin::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/postfix_admin/cli.rb

Constant Summary collapse

DEFAULT_CONFIG_PATH =
'~/.postfix_admin.conf'
MIN_NUM_PASSWORD_CHARACTER =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



12
13
14
15
# File 'lib/postfix_admin/cli.rb', line 12

def initialize
  @config = load_config
  @base = Base.new(@config)
end

Class Method Details

.config_fileObject



17
18
19
# File 'lib/postfix_admin/cli.rb', line 17

def self.config_file
  @config_file
end

.config_file=(value) ⇒ Object



21
22
23
# File 'lib/postfix_admin/cli.rb', line 21

def self.config_file=(value)
  @config_file = value
end

Instance Method Details

#add_account(address, password, name: nil, scheme: nil, rounds: nil) ⇒ Object



318
319
320
321
322
323
324
325
# File 'lib/postfix_admin/cli.rb', line 318

def (address, password, name: nil, scheme: nil, rounds: nil)
  validate_password(password)

  h_password = hashed_password(password, user_name: address,
                               scheme: scheme, rounds: rounds)
  @base.(address, h_password, name: name)
  puts_registered(address, "an account")
end

#add_admin(user_name, password, super_admin: false, scheme: nil, rounds: nil) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/postfix_admin/cli.rb', line 292

def add_admin(user_name, password, super_admin: false,
              scheme: nil, rounds: nil)
  validate_password(password)

  h_password = hashed_password(password, user_name: user_name,
                               scheme: scheme, rounds: rounds)
  @base.add_admin(user_name, h_password)

  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



308
309
310
311
# File 'lib/postfix_admin/cli.rb', line 308

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



327
328
329
330
# File 'lib/postfix_admin/cli.rb', line 327

def add_alias(address, goto)
  @base.add_alias(address, goto)
  puts_registered("#{address}: #{goto}", "an alias")
end

#add_domain(domain_name, description: nil) ⇒ Object



158
159
160
161
# File 'lib/postfix_admin/cli.rb', line 158

def add_domain(domain_name, description: nil)
  @base.add_domain(domain_name, description: description)
  puts_registered(domain_name, "a domain")
end

#change_account_password(user_name, password, scheme: nil, rounds: nil) ⇒ Object



167
168
169
# File 'lib/postfix_admin/cli.rb', line 167

def (user_name, password, scheme: nil, rounds: nil)
  change_password(Mailbox, user_name, password, scheme: scheme, rounds: rounds)
end

#change_admin_password(user_name, password, scheme: nil, rounds: nil) ⇒ Object



163
164
165
# File 'lib/postfix_admin/cli.rb', line 163

def change_admin_password(user_name, password, scheme: nil, rounds: nil)
  change_password(Admin, user_name, password, scheme: scheme, rounds: rounds)
end

#db_setupObject



25
26
27
# File 'lib/postfix_admin/cli.rb', line 25

def db_setup
  @base.db_setup
end

#delete_account(address) ⇒ Object



374
375
376
377
# File 'lib/postfix_admin/cli.rb', line 374

def (address)
  @base.(address)
  puts_deleted(address)
end

#delete_admin(user_name) ⇒ Object



369
370
371
372
# File 'lib/postfix_admin/cli.rb', line 369

def delete_admin(user_name)
  @base.delete_admin(user_name)
  puts_deleted(user_name)
end

#delete_admin_domain(user_name, domain_name) ⇒ Object



313
314
315
316
# File 'lib/postfix_admin/cli.rb', line 313

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



364
365
366
367
# File 'lib/postfix_admin/cli.rb', line 364

def delete_alias(address)
  @base.delete_alias(address)
  puts_deleted(address)
end

#delete_domain(domain_name) ⇒ Object



200
201
202
203
# File 'lib/postfix_admin/cli.rb', line 200

def delete_domain(domain_name)
  @base.delete_domain(domain_name)
  puts_deleted(domain_name)
end

#dumpObject



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/postfix_admin/cli.rb', line 400

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



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/postfix_admin/cli.rb', line 332

def (address, options)
  quota = options[:quota]
  raise "Invalid Quota value: #{quota}" if quota && quota <= 0

  mailbox_check(address)
  mailbox = Mailbox.find(address)
  mailbox.name = options[:name] if options[:name]
  mailbox.quota_mb = quota if quota
  mailbox.active = options[:active] unless options[:active].nil?
  mailbox.save!

  if options[:goto]
    mail_alias = Alias.find(address)
    mail_alias.goto = options[:goto]
    mail_alias.save!
  end

  puts "successfully updated #{address}"
  (address)
end

#edit_admin(admin_name, options) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/postfix_admin/cli.rb', line 171

def edit_admin(admin_name, options)
  admin_check(admin_name)
  admin = Admin.find(admin_name)

  unless options[:super].nil?
    admin.super_admin = options[:super]
  end

  admin.active = options[:active] unless options[:active].nil?
  admin.save!

  puts "successfully updated #{admin_name}"
  show_admin_details(admin_name)
end

#edit_alias(address, options) ⇒ Object



353
354
355
356
357
358
359
360
361
362
# File 'lib/postfix_admin/cli.rb', line 353

def edit_alias(address, options)
  alias_check(address)
  mail_alias = Alias.find(address)
  mail_alias.goto = options[:goto] if options[:goto]
  mail_alias.active = options[:active] unless options[: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



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/postfix_admin/cli.rb', line 186

def edit_domain(domain_name, options)
  domain_check(domain_name)
  domain = Domain.find(domain_name)
  domain.aliases   = options[:aliases]   if options[:aliases]
  domain.mailboxes = options[:mailboxes] if options[:mailboxes]
  domain.maxquota     = options[:maxquota]  if options[:maxquota]
  domain.active       = options[:active] unless options[:active].nil?
  domain.description  = options[:description] if options[:description]
  domain.save!

  puts "successfully updated #{domain_name}"
  show_summary(domain_name)
end

#log(domain: nil, last: nil) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/postfix_admin/cli.rb', line 379

def log(domain: nil, last: nil)
  headings = %w[Timestamp Admin Domain Action Data]
  rows = []

  logs = if domain
           Log.where(domain: domain)
         else
           Log.all
         end

  logs = logs.last(last) if last

  logs.each do |l|
    # TODO: Consider if zone should be included ('%Z').
    time = l.timestamp.strftime("%Y-%m-%d %X")
    rows << [time, l.username, l.domain, l.action, l.data]
  end

  puts_table(headings: headings, rows: rows)
end

#setup_domain(domain_name, password, scheme: nil, rounds: nil) ⇒ Object

Set up a domain Add a domain, add an admin, and grant the admin access to the domain



76
77
78
79
80
81
# File 'lib/postfix_admin/cli.rb', line 76

def setup_domain(domain_name, password, scheme: nil, rounds: nil)
  admin = "admin@#{domain_name}"
  add_domain(domain_name)
  add_admin(admin, password, scheme: scheme, rounds: rounds)
  add_admin_domain(admin, domain_name)
end

#show(name) ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
# File 'lib/postfix_admin/cli.rb', line 29

def show(name)
  name = name.downcase if name

  if name =~ /@/
    # address like argument
    if Admin.exists?(name)
      # admin
      show_admin_details(name, display_password: true)
      puts
      show_admin_domain(name)
    elsif Mailbox.exists?(name)
      # mailbox
      (name, display_password: true)
    elsif Alias.exists?(name)
      # alias
      show_alias_details(name)
    else
      raise Error, "Could not find admin/mailbox/alias #{name}"
    end

    return
  end

  show_summary(name)
  puts

  if name
    # domain name
    show_domain_details(name)
  else
    # no argument: show all domains and admins
    show_domains
    puts
    show_admins
  end
end

#show_account_details(user_name, display_password: false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/postfix_admin/cli.rb', line 91

def (user_name, display_password: false)
  (user_name)
  mailbox    = Mailbox.find(user_name)
  mail_alias = Alias.find(user_name)

  rows = []
  puts_title("Mailbox")
  rows << ["Address", mailbox.username]
  rows << ["Name", mailbox.name]
  rows << ["Password", mailbox.password] if display_password
  rows << ["Quota (MB)", mailbox.quota_display_str(format: "%.1f")]
  rows << ["Go to", mail_alias.goto]
  rows << ["Active", mailbox.active_str]

  puts_table(rows: rows)
end

#show_accounts(domain_name = nil) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/postfix_admin/cli.rb', line 225

def show_accounts(domain_name=nil)
  domain_check(domain_name) if domain_name

  rows = []
  mailboxes = if domain_name
                Domain.find(domain_name).rel_mailboxes
              else
                Mailbox.all
              end
  headings = ["No.", "Email", "Name", "Quota (MB)", "Active",
              "Scheme Prefix", "Maildir"]

  puts_title("Accounts")
  if mailboxes.empty?
    puts "No accounts"
    return
  end

  mailboxes.each_with_index do |m, i|
    no = i + 1
    rows << [no.to_s, m.username, m.name, m.quota_display_str,
             m.active_str, m.scheme_prefix, m.maildir]
  end

  puts_table(headings: headings, rows: rows)
end

#show_admin_details(name, display_password: false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/postfix_admin/cli.rb', line 108

def show_admin_details(name, display_password: false)
  admin_check(name)
  admin = Admin.find(name)

  rows = []
  puts_title("Admin")
  rows << ["Name", admin.username]
  rows << ["Password", admin.password] if display_password
  rows << ["Domains", admin.super_admin? ? "ALL" : admin.rel_domains.count.to_s]
  rows << ["Role", admin.super_admin? ? "Super Admin" : "Standard Admin"]
  rows << ["Active", admin.active_str]

  puts_table(rows: rows)
end

#show_admin_domain(user_name) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/postfix_admin/cli.rb', line 276

def show_admin_domain(user_name)
  admin = Admin.find(user_name)
  puts_title("Admin Domains (#{user_name})")
  if admin.rel_domains.empty?
    puts "\nNo domains for #{user_name}"
    return
  end

  rows = []
  admin.rel_domains.each_with_index do |d, i|
    no = i + 1
    rows << [no.to_s, d.domain]
  end
  puts_table(rows: rows, headings: %w[No. Domain])
end

#show_admins(domain_name = nil) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/postfix_admin/cli.rb', line 205

def show_admins(domain_name = nil)
  admins = domain_name ? Admin.select { |a| a.rel_domains.exists?(domain_name) } : Admin.all
  headings = ["No.", "Admin", "Domains", "Active", "Scheme Prefix"]

  puts_title("Admins")
  if admins.empty?
    puts "No admins"
    return
  end

  rows = []
  admins.each_with_index do |a, i|
    no = i + 1
    domains = a.super_admin? ? 'Super Admin' : a.rel_domains.count
    rows << [no.to_s, a.username, domains.to_s, a.active_str, a.scheme_prefix]
  end

  puts_table(headings: headings, rows: rows)
end

#show_alias_details(name) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/postfix_admin/cli.rb', line 123

def show_alias_details(name)
  alias_check(name)
  mail_alias = Alias.find(name)

  rows = []
  puts_title("Alias")
  rows << ["Address", mail_alias.address]
  rows << ["Go to", mail_alias.goto]
  rows << ["Active", mail_alias.active_str]

  puts_table(rows: rows)
end

#show_aliases(domain_name = nil) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/postfix_admin/cli.rb', line 264

def show_aliases(domain_name=nil)
  domain_check(domain_name) if domain_name

  aliases = if domain_name
              Domain.find(domain_name).rel_aliases.pure
            else
              db_aliases = Alias.pure
            end

  show_alias_base("Aliases",  aliases)
end

#show_domainsObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/postfix_admin/cli.rb', line 136

def show_domains
  rows = []
  headings = ["No.", "Domain", "Aliases", "Mailboxes","Max Quota (MB)",
              "Active", "Description"]

  puts_title("Domains")
  if Domain.without_all.empty?
    puts "No domains"
    return
  end

  Domain.without_all.each_with_index do |d, i|
    no = i + 1
    aliases_str = "%4d / %4s" % [d.pure_aliases.count, d.aliases_str]
    mailboxes_str = "%4d / %4s" % [d.rel_mailboxes.count, d.mailboxes_str]
    rows << [no.to_s, d.domain, aliases_str, mailboxes_str,
             d.maxquota_str, d.active_str, d.description]
  end

  puts_table(headings: headings, rows: rows)
end

#show_forwards(domain_name = nil) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/postfix_admin/cli.rb', line 252

def show_forwards(domain_name=nil)
  domain_check(domain_name) if domain_name

  forwards = if domain_name
               Domain.find(domain_name).rel_aliases.forward
             else
               Alias.forward
             end

  show_alias_base("Forwards", forwards)
end

#show_summary(domain_name = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/postfix_admin/cli.rb', line 66

def show_summary(domain_name = nil)
  if domain_name
    show_domain_summary(domain_name)
  else
    show_general_summary
  end
end

#teardown_domain(domain_name) ⇒ Object

Tear down a domain Delete a domain and delete an admin user for it



85
86
87
88
89
# File 'lib/postfix_admin/cli.rb', line 85

def teardown_domain(domain_name)
  admin = "admin@#{domain_name}"
  delete_domain(domain_name)
  delete_admin(admin)
end