cd59928007448a3bd77c400cecad60eb717f6365
[mailshears.git] / lib / rm / plugins / postfixadmin_db.rb
1 require 'pg'
2
3 require 'common/postfixadmin_db_plugin'
4 require 'rm/rm_plugin'
5
6 class PostfixadminDbRm
7
8 include PostfixadminDbPlugin
9 include RmPlugin
10
11
12 def delete_account(account)
13 sql_queries = ['DELETE FROM alias WHERE address = $1;']
14 # Wipe out any aliases pointed at our account.
15 sql_queries << "UPDATE alias SET goto=REPLACE(goto, $1, '');"
16 sql_queries << 'DELETE FROM mailbox WHERE username = $1;'
17 sql_queries << 'DELETE FROM quota WHERE username = $1;'
18 sql_queries << 'DELETE FROM quota2 WHERE username = $1;'
19 sql_queries << 'DELETE FROM vacation WHERE email = $1;'
20
21 # Should be handled by a trigger, according to PostfixAdmin code.
22 sql_queries << 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
23
24 begin
25 connection = PGconn.connect(@db_host,
26 @db_port,
27 @db_opts,
28 @db_tty,
29 @db_name,
30 @db_user,
31 @db_pass)
32
33 sql_queries.each do |sql_query|
34 connection.query(sql_query, [account])
35 end
36
37 # The earlier alias update query will leave things like
38 # "foo@example.com,,bar@example.com" in the "goto" column. Now
39 # we fix it. We don't do it in the loop because query() craps
40 # out on the superfluous parameter.
41 sql_query = "UPDATE alias SET goto=REPLACE(goto, ',,', ',');"
42 connection.query(sql_query)
43
44 connection.close()
45
46 rescue PGError => e
47 # Pretend like we're database-agnostic in case we ever are.
48 raise DatabaseError.new(e)
49 end
50 end
51
52
53 def delete_domain(domain)
54 sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;']
55 sql_queries << 'DELETE FROM alias WHERE domain = $1;'
56 sql_queries << 'DELETE FROM mailbox WHERE domain = $1;'
57 sql_queries << 'DELETE FROM alias_domain WHERE alias_domain = $1;'
58 sql_queries << 'DELETE FROM log WHERE domain = $1;'
59 sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
60
61 begin
62 connection = PGconn.connect(@db_host,
63 @db_port,
64 @db_opts,
65 @db_tty,
66 @db_name,
67 @db_user,
68 @db_pass)
69
70 sql_queries.each do |sql_query|
71 connection.query(sql_query, [domain])
72 end
73
74 connection.close()
75
76 rescue PGError => e
77 # Pretend like we're database-agnostic in case we ever are.
78 raise DatabaseError.new(e)
79 end
80
81 end
82
83
84
85 def get_domain_usernames(domain)
86 usernames = []
87
88 # Just assume PostgreSQL for now.
89 begin
90 connection = PGconn.connect(@db_host,
91 @db_port,
92 @db_opts,
93 @db_tty,
94 @db_name,
95 @db_user,
96 @db_pass)
97
98 sql_query = 'SELECT username FROM mailbox WHERE domain = $1;'
99
100 connection.query(sql_query, [domain]) do |result|
101 usernames = result.field_values('username')
102 end
103
104 connection.close()
105 rescue PGError => e
106 # Pretend like we're database-agnostic in case we ever are.
107 raise DatabaseError.new(e)
108 end
109
110 return usernames
111 end
112
113 end