103912b648e71ce8edf02a6be0745d76cded190b
[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.to_s()) 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 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
27 @db_name, @db_user, @db_pass)
28
29 sql_queries.each do |sql_query|
30 connection.query(sql_query, [user.to_s()])
31 end
32
33 # The earlier alias update query will leave things like
34 # "foo@example.com,,bar@example.com" in the "goto" column. Now
35 # we fix it. We don't do it in the loop because query() craps
36 # out on the superfluous parameter.
37 sql_query = "UPDATE alias SET goto=REPLACE(goto, ',,', ',');"
38 connection.query(sql_query)
39
40 connection.close()
41 end
42
43
44 def delete_domain(domain)
45 raise NonexistentDomainError.new(domain.to_s()) if not domain_exists(domain)
46
47 sql_queries = ['DELETE FROM domain_admins WHERE domain = $1;']
48 sql_queries << 'DELETE FROM alias WHERE domain = $1;'
49 sql_queries << 'DELETE FROM mailbox WHERE domain = $1;'
50 sql_queries << 'DELETE FROM alias_domain WHERE alias_domain = $1;'
51 sql_queries << 'DELETE FROM log WHERE domain = $1;'
52 sql_queries << 'DELETE FROM vacation WHERE domain = $1;'
53 sql_queries << 'DELETE FROM domain WHERE domain = $1;'
54
55 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
56 @db_name, @db_user, @db_pass)
57
58 sql_queries.each do |sql_query|
59 connection.query(sql_query, [domain.to_s()])
60 end
61
62 connection.close()
63 end
64
65
66 protected;
67
68 def domain_exists(domain)
69 count = 0
70
71 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
72 @db_name, @db_user, @db_pass)
73
74 sql_query = 'SELECT COUNT(domain) as count FROM domain WHERE domain = $1;'
75 connection.query(sql_query, [domain.to_s()]) do |result|
76 return false if result.ntuples() < 1
77 begin
78 count = result.getvalue(0,0).to_i()
79 return false if count.nil?
80 rescue StandardError
81 return false
82 end
83 end
84
85 connection.close()
86
87 return (count > 0)
88 end
89
90 end