35783022564121555c8b55a098bb31e3366bfe9d
[mailshears.git] / lib / mailshears / plugins / agendav.rb
1 require 'pg'
2
3 require 'common/plugin'
4 require 'mailshears/rm_plugin'
5
6 class AgendavDb
7
8 include Plugin
9 include RmPlugin
10
11 def initialize()
12 cfg = Configuration.new()
13 @db_host = cfg.agendav_dbhost
14 @db_port = cfg.agendav_dbport
15 @db_opts = cfg.agendav_dbopts
16 @db_tty = cfg.agendav_dbtty
17 @db_name = cfg.agendav_dbname
18 @db_user = cfg.agendav_dbuser
19 @db_pass = cfg.agendav_dbpass
20 end
21
22
23 def describe_domain(domain)
24 # AgenDAV doesn't have a concept of domains.
25 return 'N/A'
26 end
27
28 def describe_account(account)
29 if self.user_exists(account)
30 return "Username: #{account}"
31 else
32 return 'User not found'
33 end
34 end
35
36 def delete_domain(domain)
37 # AgenDAV doesn't have a concept of domains.
38 end
39
40 def delete_account(account)
41 # Delete the given username and any records in other tables
42 # belonging to it.
43
44 sql_queries = ['DELETE FROM prefs WHERE username = $1;']
45 sql_queries << 'DELETE FROM shared WHERE user_from = $1;'
46
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_queries.each do |sql_query|
57 connection.query(sql_query, [account])
58 end
59
60 connection.close()
61
62 rescue PGError => e
63 # Pretend like we're database-agnostic in case we ever are.
64 raise DatabaseError.new(e)
65 end
66
67 end
68
69
70 def get_leftover_domains(db_domains)
71 # AgenDAV doesn't have a concept of domains.
72 return []
73 end
74
75
76 def get_leftover_accounts(db_accounts)
77 # Get a list of all users who have logged in to AgenDAV.
78 ad_accounts = self.get_agendav_usernames()
79 return ad_accounts - db_accounts
80 end
81
82
83 protected;
84
85 def user_exists(account)
86 ad_users = get_agendav_usernames()
87 return ad_users.include?(account)
88 end
89
90 def get_agendav_usernames()
91 usernames = []
92
93 # Just assume PostgreSQL for now.
94 begin
95 connection = PGconn.connect(@db_host,
96 @db_port,
97 @db_opts,
98 @db_tty,
99 @db_name,
100 @db_user,
101 @db_pass)
102
103 sql_query = '(SELECT username FROM prefs)'
104 sql_query += 'UNION'
105 sql_query += '(SELECT user_from FROM shared);'
106
107 connection.query(sql_query) do |result|
108 usernames = result.field_values('username')
109 end
110
111 connection.close()
112 rescue PGError => e
113 # Pretend like we're database-agnostic in case we ever are.
114 raise DatabaseError.new(e)
115 end
116
117 return usernames
118 end
119
120 end