]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/roundcube_plugin.rb
mailshears.gemspec: bump version to 0.1.0
[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 :dbname => cfg.roundcube_dbname,
23 :user => cfg.roundcube_dbuser,
24 :password => cfg.roundcube_dbpass }
25 end
26
27
28 # Describe the given Roundcube *user*.
29 #
30 # @param user [User] the user whose description we want.
31 #
32 # @return [String] a string containing the Roundcube "User ID"
33 # associated with *user*.
34 #
35 def describe_user(user)
36 user_id = self.get_user_id(user)
37 return "User ID: #{user_id}"
38 end
39
40
41 # Return a list of Roundcube users.
42 #
43 # @return [Array<User>] a list of users contained in the
44 # Roundcube database.
45 #
46 def list_users()
47 usernames = []
48
49 connection = PG::Connection.new(@db_hash)
50
51 sql_query = 'SELECT username FROM users;'
52
53 begin
54 connection.sync_exec(sql_query) do |result|
55 usernames = result.field_values('username')
56 end
57 ensure
58 # Make sure the connection gets closed even if the query explodes.
59 connection.close()
60 end
61
62 return usernames.map{ |u| User.new(u) }
63 end
64
65 protected;
66
67
68 # Find the Roundcube "User ID" associated with the given *user*.
69 #
70 # @param user [User] the user whose Roundcube "User ID" we want.
71 #
72 # @return [Fixnum] the Roundcube "User ID" for *user*.
73 #
74 def get_user_id(user)
75 user_id = nil
76
77 connection = PG::Connection.new(@db_hash)
78 sql_query = 'SELECT user_id FROM users WHERE username = $1;'
79
80 begin
81 connection.sync_exec_params(sql_query, [user.to_s()]) do |result|
82 if result.num_tuples > 0
83 user_id = result[0]['user_id']
84 end
85 end
86 ensure
87 # Make sure the connection gets closed even if the query explodes.
88 connection.close()
89 end
90
91 return user_id
92 end
93
94
95 end