Begin building the framework to rename accounts. A pile of crap right now.
[mailshears.git] / lib / mv / plugins / roundcube_db.rb
1 require 'pg'
2
3 require 'common/plugin'
4 require 'mv/mv_plugin'
5
6 class RoundcubeDbMv
7
8 include Plugin
9 include MvPlugin
10
11 def initialize()
12 cfg = Configuration.new()
13 @db_host = cfg.roundcube_dbhost
14 @db_port = cfg.roundcube_dbport
15 @db_opts = cfg.roundcube_dbopts
16 @db_tty = cfg.roundcube_dbtty
17 @db_name = cfg.roundcube_dbname
18 @db_user = cfg.roundcube_dbuser
19 @db_pass = cfg.roundcube_dbpass
20 end
21
22
23 def describe_domain(domain)
24 # Roundcube doesn't have a concept of domains.
25 return 'N/A'
26 end
27
28 def describe_account(account)
29 user_id = self.get_user_id(account)
30
31 if user_id.nil?
32 return 'User not found'
33 else
34 return "User ID: #{user_id}"
35 end
36 end
37
38 def mv_domain(from, to)
39 # Roundcube doesn't have a concept of domains.
40 end
41
42 def mv_account(from, to)
43 sql_queries = ['UPDATE users SET username = $1 WHERE username = $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 protected;
69
70 def get_user_id(account)
71 user_id = nil
72
73 begin
74 connection = PGconn.connect(@db_host,
75 @db_port,
76 @db_opts,
77 @db_tty,
78 @db_name,
79 @db_user,
80 @db_pass)
81
82 sql_query = "SELECT user_id FROM users WHERE username = $1;"
83
84 connection.query(sql_query, [account]) do |result|
85 if result.num_tuples > 0
86 user_id = result[0]['user_id']
87 end
88 end
89
90 connection.close()
91
92 rescue PGError => e
93 # Pretend like we're database-agnostic in case we ever are.
94 raise DatabaseError.new(e)
95 end
96
97 return user_id
98 end
99
100 end