Replace 'account' with 'user' everywhere.
[mailshears.git] / lib / rm / plugins / postfixadmin.rb
1 require 'pg'
2
3 require 'common/postfixadmin_plugin'
4 require 'rm/rm_plugin'
5
6 class PostfixadminRm
7
8 include PostfixadminPlugin
9 include RmPlugin
10
11
12 def delete_user(user)
13 raise NonexistentUserError.new(user) if not user_exists(user)
14
15 sql_queries = ['DELETE FROM alias WHERE address = $1;']
16 # Wipe out any aliases pointed at our user.
17 sql_queries << "UPDATE alias SET goto=REPLACE(goto, $1, '');"
18 sql_queries << 'DELETE FROM mailbox WHERE username = $1;'
19 sql_queries << 'DELETE FROM quota WHERE username = $1;'
20 sql_queries << 'DELETE FROM quota2 WHERE username = $1;'
21 sql_queries << 'DELETE FROM vacation WHERE email = $1;'
22
23 # Should be handled by a trigger, according to PostfixAdmin code.
24 sql_queries << 'DELETE FROM vacation_notification WHERE on_vacation = $1;'
25
26 begin
27 connection = PGconn.connect(@db_host,
28 @db_port,
29 @db_opts,
30 @db_tty,
31 @db_name,
32 @db_user,
33 @db_pass)
34
35 sql_queries.each do |sql_query|
36 connection.query(sql_query, [user])
37 end
38
39 # The earlier alias update query will leave things like
40 # "foo@example.com,,bar@example.com" in the "goto" column. Now
41 # we fix it. We don't do it in the loop because query() craps
42 # out on the superfluous parameter.
43 sql_query = "UPDATE alias SET goto=REPLACE(goto, ',,', ',');"
44 connection.query(sql_query)
45
46 connection.close()
47
48 rescue PGError => e
49 # Pretend like we're database-agnostic in case we ever are.
50 raise DatabaseError.new(e)
51 end
52 end
53
54
55 def delete_domain(domain)
56 raise NonexistentDomainError.new(domain) if not domain_exists(domain)
57
58 sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;']
59 sql_queries << 'DELETE FROM alias WHERE domain = $1;'
60 sql_queries << 'DELETE FROM mailbox WHERE domain = $1;'
61 sql_queries << 'DELETE FROM alias_domain WHERE alias_domain = $1;'
62 sql_queries << 'DELETE FROM log WHERE domain = $1;'
63 sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
64 sql_queries << 'DELETE FROM domain WHERE domain = $1;'
65
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 sql_queries.each do |sql_query|
76 connection.query(sql_query, [domain])
77 end
78
79 connection.close()
80
81 rescue PGError => e
82 # Pretend like we're database-agnostic in case we ever are.
83 raise DatabaseError.new(e)
84 end
85
86 end
87
88
89 protected;
90
91 def domain_exists(domain)
92 count = 0
93
94 # Just assume PostgreSQL for now.
95 begin
96 connection = PGconn.connect(@db_host,
97 @db_port,
98 @db_opts,
99 @db_tty,
100 @db_name,
101 @db_user,
102 @db_pass)
103
104 sql_query = 'SELECT COUNT(domain) as count FROM domain WHERE domain = $1;'
105 connection.query(sql_query, [domain]) do |result|
106 return false if result.ntuples() < 1
107 begin
108 count = result.getvalue(0,0).to_i()
109 return false if count.nil?
110 rescue StandardError
111 return false
112 end
113 end
114 connection.close()
115 rescue PGError => e
116 # But pretend like we're database-agnostic in case we ever are.
117 raise DatabaseError.new(e)
118 end
119
120 return (count > 0)
121 end
122
123 end