]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/roundcube_plugin.rb
b66c9f603cf2e4c665de427d33f6210108db3b1f
[mailshears.git] / lib / common / roundcube_plugin.rb
1 require 'common/plugin'
2 require 'common/user'
3
4 # Code that all Roundcube plugins ({RoundcubePrune}, {RoundcubeRm},
5 # {RoundcubeMv}) share.
6 #
7 module RoundcubePlugin
8
9 # We implement the Plugin "interface."
10 include Plugin
11
12
13 # Initialize this Roundcube {Plugin} with values in *cfg*.
14 #
15 # @param cfg [Configuration] the configuration for this plugin.
16 #
17 def initialize(cfg)
18 @db_hash = {
19 :host => cfg.roundcube_dbhost,
20 :port => cfg.roundcube_dbport,
21 :options => cfg.roundcube_dbopts,
22 :tty => cfg.roundcube_dbtty,
23 :dbname => cfg.roundcube_dbname,
24 :user => cfg.roundcube_dbuser,
25 :password => cfg.roundcube_dbpass }
26 end
27
28
29 # Describe the given Roundcube *user*.
30 #
31 # @param user [User] the user whose description we want.
32 #
33 # @return [String] a string containing the Roundcube "User ID"
34 # associated with *user*.
35 #
36 def describe_user(user)
37 user_id = self.get_user_id(user)
38 return "User ID: #{user_id}"
39 end
40
41
42 # Return a list of Roundcube users.
43 #
44 # @return [Array<User>] a list of users contained in the
45 # Roundcube database.
46 #
47 def list_users()
48 usernames = []
49
50 connection = PG::Connection.new(@db_hash)
51
52 sql_query = 'SELECT username FROM users;'
53
54 begin
55 connection.query(sql_query) do |result|
56 usernames = result.field_values('username')
57 end
58 ensure
59 # Make sure the connection gets closed even if the query explodes.
60 connection.close()
61 end
62
63 return usernames.map{ |u| User.new(u) }
64 end
65
66 protected;
67
68
69 # Find the Roundcube "User ID" associated with the given *user*.
70 #
71 # @param user [User] the user whose Roundcube "User ID" we want.
72 #
73 # @return [Fixnum] the Roundcube "User ID" for *user*.
74 #
75 def get_user_id(user)
76 user_id = nil
77
78 connection = PG::Connection.new(@db_hash)
79 sql_query = 'SELECT user_id FROM users WHERE username = $1;'
80
81 begin
82 connection.query(sql_query, [user.to_s()]) do |result|
83 if result.num_tuples > 0
84 user_id = result[0]['user_id']
85 end
86 end
87 ensure
88 # Make sure the connection gets closed even if the query explodes.
89 connection.close()
90 end
91
92 return user_id
93 end
94
95
96 end