ae9b11c0b05d89874ebdc6dedbc2e85af36c6b89
[mailshears.git] / lib / common / agendav_plugin.rb
1 require 'common/plugin'
2
3 module AgendavPlugin
4 # Code that all Agendav plugins (Prune, Rm, Mv...) will
5 # share. That is, we implement the Plugin interface.
6 include Plugin
7
8
9 def initialize(cfg)
10 @db_host = cfg.agendav_dbhost
11 @db_port = cfg.agendav_dbport
12 @db_opts = cfg.agendav_dbopts
13 @db_tty = cfg.agendav_dbtty
14 @db_name = cfg.agendav_dbname
15 @db_user = cfg.agendav_dbuser
16 @db_pass = cfg.agendav_dbpass
17 end
18
19
20 def describe_domain(domain)
21 # AgenDAV doesn't have a concept of domains.
22 return domain
23 end
24
25
26 def describe_user(user)
27 if self.user_exists(user)
28 return "Username: #{user}"
29 else
30 return 'User not found'
31 end
32 end
33
34
35 def list_users()
36 #
37 # Produce a list of AgenDAV users. This is public because it's
38 # useful in testing.
39 #
40 usernames = []
41
42 # Just assume PostgreSQL for now.
43 begin
44 connection = PGconn.connect(@db_host,
45 @db_port,
46 @db_opts,
47 @db_tty,
48 @db_name,
49 @db_user,
50 @db_pass)
51
52 sql_query = '(SELECT username FROM prefs)'
53 sql_query += 'UNION'
54 sql_query += '(SELECT user_from FROM shared);'
55
56 connection.query(sql_query) do |result|
57 usernames = result.field_values('username')
58 end
59
60 connection.close()
61 rescue PGError => e
62 # Pretend like we're database-agnostic in case we ever are.
63 raise DatabaseError.new(e)
64 end
65
66 return usernames
67 end
68
69 end