b984f6cf66a5b2e5d66155124577f8c6a2c6eaef
[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 list_domains()
23 domains = []
24
25 # Just assume PostgreSQL for now.
26 begin
27 connection = PGconn.connect(@db_host,
28 @db_port,
29 @db_opts,
30 @db_tty,
31 @db_name,
32 @db_user,
33 @db_pass)
34
35 # 'ALL' is a magic domain, and we don't want it.
36 sql_query = "SELECT domain FROM domain WHERE domain <> 'ALL';"
37 connection.query(sql_query) do |result|
38 domains = result.field_values('domain')
39 end
40 connection.close()
41 rescue PGError => e
42 # But pretend like we're database-agnostic in case we ever are.
43 raise DatabaseError.new(e)
44 end
45
46 return domains.map{ |d| Domain.new(d) }
47 end
48
49
50
51 def list_users()
52 users = []
53
54 # Just assume PostgreSQL for now.
55 begin
56 connection = PGconn.connect(@db_host,
57 @db_port,
58 @db_opts,
59 @db_tty,
60 @db_name,
61 @db_user,
62 @db_pass)
63
64 sql_query = 'SELECT username FROM mailbox;'
65 connection.query(sql_query) do |result|
66 users = result.field_values('username')
67 end
68 connection.close()
69 rescue PGError => e
70 # But pretend like we're database-agnostic in case we ever are.
71 raise DatabaseError.new(e)
72 end
73
74 return users.map{ |u| User.new(u) }
75 end
76
77
78 def list_domains_users(domains)
79 usernames = []
80
81 # Just assume PostgreSQL for now.
82 begin
83 connection = PGconn.connect(@db_host,
84 @db_port,
85 @db_opts,
86 @db_tty,
87 @db_name,
88 @db_user,
89 @db_pass)
90
91 sql_query = 'SELECT username FROM mailbox WHERE domain IN $1;'
92
93 connection.query(sql_query, domains.map{|d| d.to_s()}) do |result|
94 usernames = result.field_values('username')
95 end
96
97 connection.close()
98 rescue PGError => e
99 # Pretend like we're database-agnostic in case we ever are.
100 raise DatabaseError.new(e)
101 end
102
103 return usernames.map{ |u| User.new(u) }
104 end
105
106
107 def list_aliases()
108 #
109 # Get a list of all aliases, useful for testing.
110 #
111 aliases = []
112
113 # Just assume PostgreSQL for now.
114 begin
115 connection = PGconn.connect(@db_host,
116 @db_port,
117 @db_opts,
118 @db_tty,
119 @db_name,
120 @db_user,
121 @db_pass)
122
123 sql_query = 'SELECT address,goto FROM alias;'
124 results = connection.query(sql_query)
125 results.each do |row|
126 aliases << row # row should be a hash
127 end
128 connection.close()
129 rescue PGError => e
130 # But pretend like we're database-agnostic in case we ever are.
131 raise DatabaseError.new(e)
132 end
133
134 return aliases
135 end
136
137 end