Class: PostfixAdmin::CLI

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

Constant Summary collapse

CONFIG_FILE =
'.postfix_admin.conf'
MIN_NUM_PASSWORD_CHARACTER =
5

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



9
10
11
12
# File 'lib/postfix_admin/cli.rb', line 9

def initialize
  @config = load_config
  @admin = PostfixAdmin.new(@config)
end

Instance Method Details

#add_account(address, password) ⇒ Object



104
105
106
107
# File 'lib/postfix_admin/cli.rb', line 104

def (address, password)
  validate_password(password)
  @admin.(address, password)
end

#add_admin(user_name, password) ⇒ Object



100
101
102
103
# File 'lib/postfix_admin/cli.rb', line 100

def add_admin(user_name, password)
  validate_password(password)
  @admin.add_admin(user_name, password)
end

#add_admin_domain(user_name, domain) ⇒ Object



111
112
113
# File 'lib/postfix_admin/cli.rb', line 111

def add_admin_domain(user_name, domain)
  @admin.add_admin_domain(user_name, domain)
end

#add_alias(address, goto) ⇒ Object



108
109
110
# File 'lib/postfix_admin/cli.rb', line 108

def add_alias(address, goto)
  @admin.add_alias(address, goto)
end

#add_domain(domain) ⇒ Object



97
98
99
# File 'lib/postfix_admin/cli.rb', line 97

def add_domain(domain)
  @admin.add_domain(domain)
end

#config_fileObject



40
41
42
# File 'lib/postfix_admin/cli.rb', line 40

def config_file
  File.expand_path(CONFIG_FILE, ENV['HOME'])
end

#create_configObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/postfix_admin/cli.rb', line 28

def create_config
  config = {
    'database'  => 'mysql://postfix:password@localhost/postfix',
    'aliases'   => 30,
    'mailboxes' => 30,
    'maxquota'  => 100
  }
  open(config_file, 'w') do |f|
    f.write config.to_yaml
  end
  File.chmod(0600, config_file)
end

#delete_domain(domain) ⇒ Object



114
115
116
# File 'lib/postfix_admin/cli.rb', line 114

def delete_domain(domain)
  @admin.delete_domain(domain)
end

#load_configObject

~/.postfix_adminrc database: mysql://postfix:password@localhost/postfix aliases: 30 mailboxes: 30 maxquota: 100



18
19
20
21
22
23
24
25
26
27
# File 'lib/postfix_admin/cli.rb', line 18

def load_config
  unless File.exist?(config_file)
    create_config
    puts "configure file: #{config_file} was generated.\nPlease execute after edit it."
    exit
  end
  open(config_file) do |f|
    YAML.load(f.read)
  end
end


43
44
45
# File 'lib/postfix_admin/cli.rb', line 43

def print_line
  puts "-"*85
end

#show_adminObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/postfix_admin/cli.rb', line 55

def show_admin
  admins = @admin.admins
  if admins.count == 0
    puts "No admin in database\n"
    return
  end
  print_line
  puts " No.   username           password"
  print_line
  admins.each_with_index do |admin, i|
    printf("%4d %-20s %10s\n", i+1, admin.username, admin.password)
  end
  print_line
end

#show_admin_domain(user_name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/postfix_admin/cli.rb', line 83

def show_admin_domain(user_name)
  domain_admins = @admin.admin_domains(user_name)
  if domain_admins.count == 0
    puts "No domain in database\n"
    return
  end
  print_line
  puts " No.   domain"
  print_line
  domain_admins.each_with_index do |domain_admin, i|
    printf("%4d %-20s\n", i+1, domain_admin.domain)
  end
  print_line
end

#show_domainObject



46
47
48
49
50
51
52
53
54
# File 'lib/postfix_admin/cli.rb', line 46

def show_domain
  print_line
  puts " No.   domain             alias mail quota"
  print_line
  @admin.domains.each_with_index do |domain, i|
    printf("%4d %-20s %4d %4d %4d\n", i+1, domain.domain, domain.aliases, domain.mailboxes, domain.maxquota)
  end
  print_line
end

#show_domain_account(domain) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/postfix_admin/cli.rb', line 69

def (domain)
  mailboxes = @admin.mailboxes(domain)
  if mailboxes.count == 0
    puts "No address in #{domain}\n"
    return
  end
  print_line
  puts " No.   address             password  quota(M) maildir"
  print_line
  mailboxes.each_with_index do |mailbox, i|
    printf("%4d %-20s %10s %7.1f  %-30s\n", i+1, mailbox.username, mailbox.password, mailbox.quota.to_f/1024000.0, mailbox.maildir)
  end
  print_line
end

#validate_password(password) ⇒ Object



117
118
119
120
121
# File 'lib/postfix_admin/cli.rb', line 117

def validate_password(password)
  if password.size < MIN_NUM_PASSWORD_CHARACTER
    raise "Error: Password is too short. It should be larger than #{MIN_NUM_PASSWORD_CHARACTER}"
  end
end