]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/agendav_plugin.rb
mailshears.gemspec: update the version to 0.0.5.
[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 are also "owner" and "with" fields in the "shares" table,
39 # but they contains principal URLs and not a bare username. Thus
40 # their format depends on the CalDAV server configuration, and
41 # isn't predictable.
42 sql_query = 'SELECT username FROM prefs;'
43
44 begin
45 connection.sync_exec(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
57 # Count the number of rows in the "shares" table. Used only for
58 # testing.
59 #
60 # @return [Fixnum] the number of rows in the "shares" table.
61 #
62 def count_shares()
63 count = nil
64 connection = PG::Connection.new(@db_hash)
65
66 sql_query = 'SELECT count(*) FROM shares;'
67 begin
68 connection.sync_exec(sql_query) do |result|
69 count = result.getvalue(0,0).to_i()
70 end
71 ensure
72 # Make sure the connection gets closed even if the query explodes.
73 connection.close()
74 end
75
76 return count
77 end
78
79 end