]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/postfixadmin_plugin.rb
60880d9e7bad17cf2298b4b0c9498d1007258039
[mailshears.git] / lib / common / postfixadmin_plugin.rb
1 require 'common/domain'
2 require 'common/plugin'
3 require 'common/user'
4 require 'pg'
5
6 module PostfixadminPlugin
7 # Code that all Postfixadmin plugins (Prune, Rm, Mv...) will
8 # share. That is, we implement the Plugin interface.
9 include Plugin
10
11 def initialize(cfg)
12 @db_host = cfg.postfixadmin_dbhost
13 @db_port = cfg.postfixadmin_dbport
14 @db_opts = cfg.postfixadmin_dbopts
15 @db_tty = cfg.postfixadmin_dbtty
16 @db_name = cfg.postfixadmin_dbname
17 @db_user = cfg.postfixadmin_dbuser
18 @db_pass = cfg.postfixadmin_dbpass
19 end
20
21
22 def describe_user(user)
23 # There's no other unique identifier in PostfixAdmin
24 return user.to_s()
25 end
26
27
28 def describe_domain(domain)
29 # There's no other unique identifier in PostfixAdmin
30 return domain.to_s()
31 end
32
33
34 def list_domains()
35 domains = []
36
37 # Just assume PostgreSQL for now.
38 begin
39 connection = PGconn.connect(@db_host,
40 @db_port,
41 @db_opts,
42 @db_tty,
43 @db_name,
44 @db_user,
45 @db_pass)
46
47 # 'ALL' is a magic domain, and we don't want it.
48 sql_query = "SELECT domain FROM domain WHERE domain <> 'ALL';"
49 connection.query(sql_query) do |result|
50 domains = result.field_values('domain')
51 end
52 connection.close()
53 rescue PGError => e
54 # But pretend like we're database-agnostic in case we ever are.
55 raise DatabaseError.new(e)
56 end
57
58 return domains.map{ |d| Domain.new(d) }
59 end
60
61
62 def domain_exists(domain)
63 # Does the given domain exist in Postfixadmin? We use a naive
64 # implementation here based on list_domains(). This isn't in our
65 # superclass because not all plugins have a concept of domains.
66 domains = list_domains()
67 return domains.include?(domain)
68 end
69
70
71 def list_users()
72 users = []
73
74 # Just assume PostgreSQL for now.
75 begin
76 connection = PGconn.connect(@db_host,
77 @db_port,
78 @db_opts,
79 @db_tty,
80 @db_name,
81 @db_user,
82 @db_pass)
83
84 sql_query = 'SELECT username FROM mailbox;'
85 connection.query(sql_query) do |result|
86 users = result.field_values('username')
87 end
88 connection.close()
89 rescue PGError => e
90 # But pretend like we're database-agnostic in case we ever are.
91 raise DatabaseError.new(e)
92 end
93
94 return users.map{ |u| User.new(u) }
95 end
96
97
98 def list_domains_users(domains)
99 usernames = []
100
101 # Just assume PostgreSQL for now.
102 begin
103 connection = PGconn.connect(@db_host,
104 @db_port,
105 @db_opts,
106 @db_tty,
107 @db_name,
108 @db_user,
109 @db_pass)
110
111 sql_query = 'SELECT username FROM mailbox WHERE domain IN $1;'
112
113 connection.query(sql_query, domains.map{|d| d.to_s()}) do |result|
114 usernames = result.field_values('username')
115 end
116
117 connection.close()
118 rescue PGError => e
119 # Pretend like we're database-agnostic in case we ever are.
120 raise DatabaseError.new(e)
121 end
122
123 return usernames.map{ |u| User.new(u) }
124 end
125
126
127 def list_aliases()
128 #
129 # Get a list of all aliases, useful for testing.
130 #
131 aliases = []
132
133 # Just assume PostgreSQL for now.
134 begin
135 connection = PGconn.connect(@db_host,
136 @db_port,
137 @db_opts,
138 @db_tty,
139 @db_name,
140 @db_user,
141 @db_pass)
142
143 sql_query = 'SELECT address,goto FROM alias;'
144 results = connection.query(sql_query)
145 results.each do |row|
146 aliases << row # row should be a hash
147 end
148 connection.close()
149 rescue PGError => e
150 # But pretend like we're database-agnostic in case we ever are.
151 raise DatabaseError.new(e)
152 end
153
154 return aliases
155 end
156
157 end