Pass a Configuration object to each plugin's initialize().
[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_account(account)
27 if self.user_exists(account)
28 return "Username: #{account}"
29 else
30 return 'User not found'
31 end
32 end
33
34
35 protected;
36
37
38 def list_users()
39 usernames = []
40
41 # Just assume PostgreSQL for now.
42 begin
43 connection = PGconn.connect(@db_host,
44 @db_port,
45 @db_opts,
46 @db_tty,
47 @db_name,
48 @db_user,
49 @db_pass)
50
51 sql_query = '(SELECT username FROM prefs)'
52 sql_query += 'UNION'
53 sql_query += '(SELECT user_from FROM shared);'
54
55 connection.query(sql_query) do |result|
56 usernames = result.field_values('username')
57 end
58
59 connection.close()
60 rescue PGError => e
61 # Pretend like we're database-agnostic in case we ever are.
62 raise DatabaseError.new(e)
63 end
64
65 return usernames
66 end
67
68 end