fad869f95235964884d58635a465d670cde7b455
[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()
10 cfg = Configuration.new()
11 @db_host = cfg.agendav_dbhost
12 @db_port = cfg.agendav_dbport
13 @db_opts = cfg.agendav_dbopts
14 @db_tty = cfg.agendav_dbtty
15 @db_name = cfg.agendav_dbname
16 @db_user = cfg.agendav_dbuser
17 @db_pass = cfg.agendav_dbpass
18 end
19
20
21 def describe_domain(domain)
22 # AgenDAV doesn't have a concept of domains.
23 return 'N/A'
24 end
25
26
27 def describe_account(account)
28 if self.user_exists(account)
29 return "Username: #{account}"
30 else
31 return 'User not found'
32 end
33 end
34
35
36 protected;
37
38 def user_exists(account)
39 ad_users = get_agendav_usernames()
40 return ad_users.include?(account)
41 end
42
43 def get_agendav_usernames()
44 usernames = []
45
46 # Just assume PostgreSQL for now.
47 begin
48 connection = PGconn.connect(@db_host,
49 @db_port,
50 @db_opts,
51 @db_tty,
52 @db_name,
53 @db_user,
54 @db_pass)
55
56 sql_query = '(SELECT username FROM prefs)'
57 sql_query += 'UNION'
58 sql_query += '(SELECT user_from FROM shared);'
59
60 connection.query(sql_query) do |result|
61 usernames = result.field_values('username')
62 end
63
64 connection.close()
65 rescue PGError => e
66 # Pretend like we're database-agnostic in case we ever are.
67 raise DatabaseError.new(e)
68 end
69
70 return usernames
71 end
72
73 end