364b8199e23f992c2a4744ac3ec76239a98b7714
[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 end