]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/postfixadmin_plugin.rb
4bfda9dcdece7e7216856b17ff96745c031313e7
[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()
10
11 cfg = Configuration.new()
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_account(account)
23 # There's no other unique identifier in PostfixAdmin
24 return account
25 end
26
27
28 def describe_domain(domain)
29 # There's no other unique identifier in PostfixAdmin
30 return domain
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
59 end
60
61
62 def list_users()
63 accounts = []
64
65 # Just assume PostgreSQL for now.
66 begin
67 connection = PGconn.connect(@db_host,
68 @db_port,
69 @db_opts,
70 @db_tty,
71 @db_name,
72 @db_user,
73 @db_pass)
74
75 # If address = goto, then the alias basically says, "really
76 # deliver to that address; it's not an alias."
77 sql_query = 'SELECT username FROM mailbox;'
78 connection.query(sql_query) do |result|
79 accounts = result.field_values('username')
80 end
81 connection.close()
82 rescue PGError => e
83 # But pretend like we're database-agnostic in case we ever are.
84 raise DatabaseError.new(e)
85 end
86
87 return accounts
88 end
89
90
91 def list_domains_users(domains)
92 usernames = []
93
94 # Just assume PostgreSQL for now.
95 begin
96 connection = PGconn.connect(@db_host,
97 @db_port,
98 @db_opts,
99 @db_tty,
100 @db_name,
101 @db_user,
102 @db_pass)
103
104 sql_query = 'SELECT username FROM mailbox WHERE domain IN $1;'
105
106 connection.query(sql_query, [domains]) do |result|
107 usernames = result.field_values('username')
108 end
109
110 connection.close()
111 rescue PGError => e
112 # Pretend like we're database-agnostic in case we ever are.
113 raise DatabaseError.new(e)
114 end
115
116 return usernames
117 end
118
119 end