Stop pretending that we'll ever work with another DBMS.
[mailshears.git] / lib / common / roundcube_plugin.rb
1 require 'common/plugin'
2 require 'common/user'
3
4 module RoundcubePlugin
5 # Code that all Roundcube plugins (Prune, Rm, Mv...) will share.
6 # That is, we implement the Plugin interface.
7 include Plugin
8
9 def initialize(cfg)
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_user(user)
21 user_id = self.get_user_id(user)
22 return "User ID: #{user_id}"
23 end
24
25
26 def list_users()
27 # Produce a list of Roundcube users. This is used in prune/rm, and
28 # is public because it is useful in testing.
29 usernames = []
30
31 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
32 @db_name, @db_user, @db_pass)
33
34 sql_query = 'SELECT username FROM users;'
35 connection.query(sql_query) do |result|
36 usernames = result.field_values('username')
37 end
38
39 connection.close()
40
41 return usernames.map{ |u| User.new(u) }
42 end
43
44 protected;
45
46 def get_user_id(user)
47 user_id = nil
48
49 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
50 @db_name, @db_user, @db_pass)
51
52 sql_query = 'SELECT user_id FROM users WHERE username = $1;'
53
54 connection.query(sql_query, [user.to_s()]) do |result|
55 if result.num_tuples > 0
56 user_id = result[0]['user_id']
57 end
58 end
59
60 connection.close()
61
62 return user_id
63 end
64
65
66 end