]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/roundcube_db_plugin.rb
Way too many changes to mention. The 'rm' mode works now.
[mailshears.git] / lib / common / roundcube_db_plugin.rb
1 require 'common/plugin'
2
3 module RoundcubeDbPlugin
4 # Code that all RoundcubeDb plugins (Prune, Rm, Mv...) will share.
5 # That is, we implement the Plugin interface.
6 include Plugin
7
8 def initialize()
9 cfg = Configuration.new()
10 @db_host = cfg.roundcube_dbhost
11 @db_port = cfg.roundcube_dbport
12 @db_opts = cfg.roundcube_dbopts
13 @db_tty = cfg.roundcube_dbtty
14 @db_name = cfg.roundcube_dbname
15 @db_user = cfg.roundcube_dbuser
16 @db_pass = cfg.roundcube_dbpass
17 end
18
19
20 def describe_domain(domain)
21 # Roundcube doesn't have a concept of domains.
22 return 'N/A'
23 end
24
25 def describe_account(account)
26 user_id = self.get_user_id(account)
27
28 if user_id.nil?
29 return 'User not found'
30 else
31 return "User ID: #{user_id}"
32 end
33 end
34
35
36 protected;
37
38 def get_user_id(account)
39 user_id = nil
40
41 begin
42 connection = PGconn.connect(@db_host,
43 @db_port,
44 @db_opts,
45 @db_tty,
46 @db_name,
47 @db_user,
48 @db_pass)
49
50 sql_query = "SELECT user_id FROM users WHERE username = $1;"
51
52 connection.query(sql_query, [account]) do |result|
53 if result.num_tuples > 0
54 user_id = result[0]['user_id']
55 end
56 end
57
58 connection.close()
59
60 rescue PGError => e
61 # Pretend like we're database-agnostic in case we ever are.
62 raise DatabaseError.new(e)
63 end
64
65 return user_id
66 end
67
68
69 # Uses in both prune/rm.
70 def get_roundcube_usernames()
71 usernames = []
72
73 # Just assume PostgreSQL for now.
74 begin
75 connection = PGconn.connect(@db_host,
76 @db_port,
77 @db_opts,
78 @db_tty,
79 @db_name,
80 @db_user,
81 @db_pass)
82
83 sql_query = "SELECT username FROM users;"
84 connection.query(sql_query) do |result|
85 usernames = result.field_values('username')
86 end
87
88 connection.close()
89 rescue PGError => e
90 # Pretend like we're database-agnostic in case we ever are.
91 raise DatabaseError.new(e)
92 end
93
94 return usernames
95 end
96
97 end