]> gitweb.michael.orlitzky.com - mailshears.git/blob - src/postfixadmin_db.rb
Convert DovecotMailstore to a plugin, and generalize the main mailshears script to...
[mailshears.git] / src / postfixadmin_db.rb
1 require 'postgres'
2
3 class PostfixadminDb
4
5 def initialize(db_host,
6 db_port,
7 db_opts,
8 db_tty,
9 db_name,
10 db_user,
11 db_pass)
12
13 @db_host = db_host
14 @db_port = db_port
15 @db_opts = db_opts
16 @db_tty = db_tty
17 @db_name = db_name
18 @db_user = db_user
19 @db_pass = db_pass
20 end
21
22
23 def get_domains_from_db()
24 # Just assume PostgreSQL for now.
25 begin
26 connection = PGconn.connect(@db_host,
27 @db_port,
28 @db_opts,
29 @db_tty,
30 @db_name,
31 @db_user,
32 @db_pass)
33
34 # 'ALL' is a magic domain, and we don't want it.
35 sql_query = "SELECT domain FROM domain WHERE domain <> 'ALL';"
36 result = connection.query(sql_query)
37 connection.close()
38 rescue PGError => e
39 # But pretend like we're database-agnostic in case we ever are.
40 raise DatabaseError.new(e)
41 end
42
43 # The database query returns an array of rows. Since we only asked
44 # for one column (domain), we can flatten the result into an
45 # array of domains.
46 return result.flatten
47 end
48
49
50 def get_accounts_from_db()
51 # Just assume PostgreSQL for now.
52 begin
53 connection = PGconn.connect(@db_host,
54 @db_port,
55 @db_opts,
56 @db_tty,
57 @db_name,
58 @db_user,
59 @db_pass)
60
61 # If address = goto, then the alias basically says, "really
62 # deliver to that address; it's not an alias."
63 sql_query = 'SELECT username FROM mailbox;'
64 result = connection.query(sql_query)
65 connection.close()
66 rescue PGError => e
67 # But pretend like we're database-agnostic in case we ever are.
68 raise DatabaseError.new(e)
69 end
70
71 # The database query returns an array of rows. Since we only asked
72 # for one column (address), we can flatten the result into an
73 # array of addresses.
74 return result.flatten
75 end
76
77 end