Way too many changes to mention. The 'rm' mode works now.
[mailshears.git] / lib / rm / plugins / roundcube_db.rb
1 require 'pg'
2
3 require 'common/roundcube_db_plugin'
4 require 'rm/rm_plugin'
5
6 class RoundcubeDbRm
7
8 include RoundcubeDbPlugin
9 include RmPlugin
10
11 def delete_domain(domain)
12 # Roundcube doesn't have a concept of domains.
13 end
14
15 def delete_account(account)
16 # Delete the given username and any records in other tables
17 # belonging to it.
18 user_id = self.get_user_id(account)
19
20 # The Roundcube developers were nice enough to include
21 # DBMS-specific install and upgrade scripts, so Postgres can take
22 # advantage of ON DELETE triggers. Here's an example:
23 #
24 # ...
25 # user_id integer NOT NULL
26 # REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE CASCADE
27 #
28 # This query is of course necessary with any DBMS:
29 sql_queries = ['DELETE FROM users WHERE user_id = $1::int;']
30
31 begin
32 connection = PGconn.connect(@db_host,
33 @db_port,
34 @db_opts,
35 @db_tty,
36 @db_name,
37 @db_user,
38 @db_pass)
39
40 sql_queries.each do |sql_query|
41 connection.query(sql_query, [user_id])
42 end
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
51 end
52
53
54
55 def get_domain_usernames(domain)
56 usernames = get_roundcube_usernames();
57 matches = usernames.select do |username|
58 username =~ /@#{domain}$/
59 end
60
61 return matches
62 end
63
64 end