]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/agendav_plugin.rb
mailshears.gemspec: bump version to 0.1.0
[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 :dbname => cfg.agendav_dbname,
22 :user => cfg.agendav_dbuser,
23 :password => cfg.agendav_dbpass }
24 end
25
26
27 # Return a list of Agendav users.
28 #
29 # @return [Array<User>] a list of users contained in the
30 # Agendav database.
31 #
32 def list_users()
33 users = []
34
35 connection = PG::Connection.new(@db_hash)
36
37 # There are also "owner" and "with" fields in the "shares" table,
38 # but they contains principal URLs and not a bare username. Thus
39 # their format depends on the CalDAV server configuration, and
40 # isn't predictable.
41 sql_query = 'SELECT username FROM prefs;'
42
43 begin
44 connection.sync_exec(sql_query) do |result|
45 users = result.field_values('username')
46 end
47 ensure
48 # Make sure the connection gets closed even if the query explodes.
49 connection.close()
50 end
51
52 return users.map{ |u| User.new(u) }
53 end
54
55
56 # Count the number of rows in the "shares" table. Used only for
57 # testing.
58 #
59 # @return [Fixnum] the number of rows in the "shares" table.
60 #
61 def count_shares()
62 count = nil
63 connection = PG::Connection.new(@db_hash)
64
65 sql_query = 'SELECT count(*) FROM shares;'
66 begin
67 connection.sync_exec(sql_query) do |result|
68 count = result.getvalue(0,0).to_i()
69 end
70 ensure
71 # Make sure the connection gets closed even if the query explodes.
72 connection.close()
73 end
74
75 return count
76 end
77
78 end