]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/postfixadmin_plugin.rb
1 require 'common/domain'
2 require 'common/plugin'
6 module PostfixadminPlugin
7 # Code that all Postfixadmin plugins (Prune, Rm, Mv...) will
8 # share. That is, we implement the Plugin interface.
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
22 def describe_user(user
)
23 # There's no other unique identifier in PostfixAdmin
28 def describe_domain(domain
)
29 # There's no other unique identifier in PostfixAdmin
37 # Just assume PostgreSQL for now.
39 connection
= PGconn
.connect(@db_host,
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')
54 # But pretend like we're database-agnostic in case we ever are.
55 raise DatabaseError
.new(e
)
58 return domains
.map
{ |d
| Domain
.new(d
) }
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
)
74 # Just assume PostgreSQL for now.
76 connection
= PGconn
.connect(@db_host,
84 sql_query
= 'SELECT username FROM mailbox;'
85 connection
.query(sql_query
) do |result
|
86 users
= result
.field_values('username')
90 # But pretend like we're database-agnostic in case we ever are.
91 raise DatabaseError
.new(e
)
94 return users
.map
{ |u
| User
.new(u
) }
98 def list_domains_users(domains
)
101 # Just assume PostgreSQL for now.
103 connection
= PGconn
.connect(@db_host,
111 sql_query
= 'SELECT username FROM mailbox WHERE domain IN $1;'
113 connection
.query(sql_query
, domains
.map
{|d
| d
.to_s()}) do |result
|
114 usernames
= result
.field_values('username')
119 # Pretend like we're database-agnostic in case we ever are.
120 raise DatabaseError
.new(e
)
123 return usernames
.map
{ |u
| User
.new(u
) }
129 # Get a list of all aliases, useful for testing.
133 # Just assume PostgreSQL for now.
135 connection
= PGconn
.connect(@db_host,
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
150 # But pretend like we're database-agnostic in case we ever are.
151 raise DatabaseError
.new(e
)