Way too many changes to mention. The 'rm' mode works now.
[mailshears.git] / lib / common / postfixadmin_db_plugin.rb
1 require 'common/plugin'
2 require 'pg'
3
4 module PostfixadminDbPlugin
5 # Code that all PostfixadminDb 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.dbhost
13 @db_port = cfg.dbport
14 @db_opts = cfg.dbopts
15 @db_tty = cfg.dbtty
16 @db_name = cfg.dbname
17 @db_user = cfg.dbuser
18 @db_pass = cfg.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 get_domains_from_db()
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 get_accounts_from_db()
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 end