031e3f9ddd725e6b2bc427a323e567c1291300e3
[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_host = cfg.roundcube_dbhost
19 @db_port = cfg.roundcube_dbport
20 @db_opts = cfg.roundcube_dbopts
21 @db_tty = cfg.roundcube_dbtty
22 @db_name = cfg.roundcube_dbname
23 @db_user = cfg.roundcube_dbuser
24 @db_pass = 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 = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
50 @db_name, @db_user, @db_pass)
51
52 sql_query = 'SELECT username FROM users;'
53 connection.query(sql_query) do |result|
54 usernames = result.field_values('username')
55 end
56
57 connection.close()
58
59 return usernames.map{ |u| User.new(u) }
60 end
61
62 protected;
63
64
65 # Find the Roundcube "User ID" associated with the given *user*.
66 #
67 # @param user [User] the user whose Roundcube "User ID" we want.
68 #
69 # @return [Fixnum] the Roundcube "User ID" for *user*.
70 #
71 def get_user_id(user)
72 user_id = nil
73
74 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
75 @db_name, @db_user, @db_pass)
76
77 sql_query = 'SELECT user_id FROM users WHERE username = $1;'
78
79 connection.query(sql_query, [user.to_s()]) do |result|
80 if result.num_tuples > 0
81 user_id = result[0]['user_id']
82 end
83 end
84
85 connection.close()
86
87 return user_id
88 end
89
90
91 end