]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/postfixadmin_db.rb
0249ea07949b384a5438f34c9c5f7c27e582d7e0
[mailshears.git] / lib / postfixadmin_db.rb
1 require 'pg'
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 domains = []
25
26 # Just assume PostgreSQL for now.
27 begin
28 connection = PGconn.connect(@db_host,
29 @db_port,
30 @db_opts,
31 @db_tty,
32 @db_name,
33 @db_user,
34 @db_pass)
35
36 # 'ALL' is a magic domain, and we don't want it.
37 sql_query = "SELECT domain FROM domain WHERE domain <> 'ALL';"
38 connection.query(sql_query) do |result|
39 domains = result.field_values('domain')
40 end
41 connection.close()
42 rescue PGError => e
43 # But pretend like we're database-agnostic in case we ever are.
44 raise DatabaseError.new(e)
45 end
46
47 return domains
48 end
49
50
51 def get_accounts_from_db()
52 accounts = []
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 # If address = goto, then the alias basically says, "really
65 # deliver to that address; it's not an alias."
66 sql_query = 'SELECT username FROM mailbox;'
67 connection.query(sql_query) do |result|
68 accounts = result.field_values('username')
69 end
70 connection.close()
71 rescue PGError => e
72 # But pretend like we're database-agnostic in case we ever are.
73 raise DatabaseError.new(e)
74 end
75
76 return accounts
77 end
78
79 end