]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/postfixadmin_plugin.rb
5556d5a7c23a15866a0a7fb17e6cbb77f2a21f1d
[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 # If address = goto, then the alias basically says, "really
74 # deliver to that address; it's not an alias."
75 sql_query = 'SELECT username FROM mailbox;'
76 connection.query(sql_query) do |result|
77 users = result.field_values('username')
78 end
79 connection.close()
80 rescue PGError => e
81 # But pretend like we're database-agnostic in case we ever are.
82 raise DatabaseError.new(e)
83 end
84
85 return users
86 end
87
88
89 def list_domains_users(domains)
90 usernames = []
91
92 # Just assume PostgreSQL for now.
93 begin
94 connection = PGconn.connect(@db_host,
95 @db_port,
96 @db_opts,
97 @db_tty,
98 @db_name,
99 @db_user,
100 @db_pass)
101
102 sql_query = 'SELECT username FROM mailbox WHERE domain IN $1;'
103
104 connection.query(sql_query, [domains]) do |result|
105 usernames = result.field_values('username')
106 end
107
108 connection.close()
109 rescue PGError => e
110 # Pretend like we're database-agnostic in case we ever are.
111 raise DatabaseError.new(e)
112 end
113
114 return usernames
115 end
116
117 end