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