Begin building the framework to rename accounts. A pile of crap right now.
[mailshears.git] / lib / mv / plugins / agendav.rb
1 require 'pg'
2
3 require 'common/plugin'
4 require 'mv/mv_plugin'
5
6 class AgendavMv
7
8 include Plugin
9 include MvPlugin
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 mv_domain(from, to)
37 # AgenDAV doesn't have a concept of domains.
38 end
39
40 def mv_account(from, to)
41 sql_queries = ['UPDATE prefs SET username = $1 WHERE username $2;']
42 sql_queries << 'UPDATE shared SET user_from = $1 WHERE user_from = $2;'
43 sql_queries << 'UPDATE shared SET user_which = $1 WHERE user_which = $2;'
44
45 begin
46 connection = PGconn.connect(@db_host,
47 @db_port,
48 @db_opts,
49 @db_tty,
50 @db_name,
51 @db_user,
52 @db_pass)
53
54 sql_queries.each do |sql_query|
55 connection.query(sql_query, [to, from])
56 end
57
58 connection.close()
59
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 end
66
67
68
69 protected;
70
71 def user_exists(account)
72 ad_users = get_agendav_usernames()
73 return ad_users.include?(account)
74 end
75
76 def get_agendav_usernames()
77 usernames = []
78
79 # Just assume PostgreSQL for now.
80 begin
81 connection = PGconn.connect(@db_host,
82 @db_port,
83 @db_opts,
84 @db_tty,
85 @db_name,
86 @db_user,
87 @db_pass)
88
89 sql_query = '(SELECT username FROM prefs)'
90 sql_query += 'UNION'
91 sql_query += '(SELECT user_from FROM shared);'
92
93 connection.query(sql_query) do |result|
94 usernames = result.field_values('username')
95 end
96
97 connection.close()
98 rescue PGError => e
99 # Pretend like we're database-agnostic in case we ever are.
100 raise DatabaseError.new(e)
101 end
102
103 return usernames
104 end
105
106 end