]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/postfixadmin_plugin.rb
Add domain_exists() to PostfixadminPlugin.
[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 domain_exists(domain)
61 # Does the given domain exist in Postfixadmin? We use a naive
62 # implementation here based on list_domains(). This isn't in our
63 # superclass because not all plugins have a concept of domains.
64 domains = list_domains()
65 return domains.include?(domain)
66 end
67
68
69 def list_users()
70 users = []
71
72 # Just assume PostgreSQL for now.
73 begin
74 connection = PGconn.connect(@db_host,
75 @db_port,
76 @db_opts,
77 @db_tty,
78 @db_name,
79 @db_user,
80 @db_pass)
81
82 sql_query = 'SELECT username FROM mailbox;'
83 connection.query(sql_query) do |result|
84 users = result.field_values('username')
85 end
86 connection.close()
87 rescue PGError => e
88 # But pretend like we're database-agnostic in case we ever are.
89 raise DatabaseError.new(e)
90 end
91
92 return users
93 end
94
95
96 def list_domains_users(domains)
97 usernames = []
98
99 # Just assume PostgreSQL for now.
100 begin
101 connection = PGconn.connect(@db_host,
102 @db_port,
103 @db_opts,
104 @db_tty,
105 @db_name,
106 @db_user,
107 @db_pass)
108
109 sql_query = 'SELECT username FROM mailbox WHERE domain IN $1;'
110
111 connection.query(sql_query, [domains]) do |result|
112 usernames = result.field_values('username')
113 end
114
115 connection.close()
116 rescue PGError => e
117 # Pretend like we're database-agnostic in case we ever are.
118 raise DatabaseError.new(e)
119 end
120
121 return usernames
122 end
123
124
125 def list_aliases()
126 #
127 # Get a list of all aliases, useful for testing.
128 #
129 aliases = []
130
131 # Just assume PostgreSQL for now.
132 begin
133 connection = PGconn.connect(@db_host,
134 @db_port,
135 @db_opts,
136 @db_tty,
137 @db_name,
138 @db_user,
139 @db_pass)
140
141 sql_query = 'SELECT address,goto FROM alias;'
142 results = connection.query(sql_query)
143 results.each do |row|
144 aliases << row # row should be a hash
145 end
146 connection.close()
147 rescue PGError => e
148 # But pretend like we're database-agnostic in case we ever are.
149 raise DatabaseError.new(e)
150 end
151
152 return aliases
153 end
154
155 end