]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/agendav_plugin.rb
Update AgenDAV fixtures for the v2.1.0 schema.
[mailshears.git] / lib / common / agendav_plugin.rb
1 require 'common/plugin'
2 require 'common/user'
3
4 # Code that all Agendav plugins ({AgendavPrune}, {AgendavRm},
5 # {AgendavMv}) share.
6 module AgendavPlugin
7
8 # We implement the Plugin "interface."
9 include Plugin
10
11
12 # Initialize this Agendav {Plugin} with values in *cfg*.
13 #
14 # @param cfg [Configuration] the configuration for this plugin.
15 #
16 def initialize(cfg)
17 @db_hash = {
18 :host => cfg.agendav_dbhost,
19 :port => cfg.agendav_dbport,
20 :options => cfg.agendav_dbopts,
21 :tty => cfg.agendav_dbtty,
22 :dbname => cfg.agendav_dbname,
23 :user => cfg.agendav_dbuser,
24 :password => cfg.agendav_dbpass }
25 end
26
27
28 # Return a list of Agendav users.
29 #
30 # @return [Array<User>] a list of users contained in the
31 # Agendav database.
32 #
33 def list_users()
34 users = []
35
36 connection = PG::Connection.new(@db_hash)
37
38 # There's also an "owner" field in the "shares" table, but it
39 # contains a principal URL and not a bare username. Thus its
40 # format depends on the CalDAV server configuration, and isn't
41 # predictable.
42 sql_query = 'SELECT username FROM prefs'
43
44 begin
45 connection.query(sql_query) do |result|
46 users = result.field_values('username')
47 end
48 ensure
49 # Make sure the connection gets closed even if the query explodes.
50 connection.close()
51 end
52
53 return users.map{ |u| User.new(u) }
54 end
55
56 end