Move domain removal into the plugins.
[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 raise NonexistentDomainError.new(domain) if not domain_exists(domain)
55
56 sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;']
57 sql_queries << 'DELETE FROM alias WHERE domain = $1;'
58 sql_queries << 'DELETE FROM mailbox WHERE domain = $1;'
59 sql_queries << 'DELETE FROM alias_domain WHERE alias_domain = $1;'
60 sql_queries << 'DELETE FROM log WHERE domain = $1;'
61 sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
62 sql_queries << 'DELETE FROM domain WHERE domain = $1;'
63
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 sql_queries.each do |sql_query|
74 connection.query(sql_query, [domain])
75 end
76
77 connection.close()
78
79 rescue PGError => e
80 # Pretend like we're database-agnostic in case we ever are.
81 raise DatabaseError.new(e)
82 end
83
84 end
85
86
87 protected;
88
89 def domain_exists(domain)
90 count = 0
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 COUNT(domain) as count FROM domain WHERE domain = $1;'
103 connection.query(sql_query, [domain]) do |result|
104 return false if result.ntuples() < 1
105 begin
106 count = result.getvalue(0,0).to_i()
107 return false if count.nil?
108 rescue StandardError
109 return false
110 end
111 end
112 connection.close()
113 rescue PGError => e
114 # But pretend like we're database-agnostic in case we ever are.
115 raise DatabaseError.new(e)
116 end
117
118 return (count > 0)
119 end
120
121 end