1f9805a3e9d079243da75859f6992d7688e6f266
[mailshears.git] / lib / common / roundcube_plugin.rb
1 require 'common/plugin'
2
3 module RoundcubePlugin
4 # Code that all Roundcube plugins (Prune, Rm, Mv...) will share.
5 # That is, we implement the Plugin interface.
6 include Plugin
7
8 def initialize(cfg)
9 @db_host = cfg.roundcube_dbhost
10 @db_port = cfg.roundcube_dbport
11 @db_opts = cfg.roundcube_dbopts
12 @db_tty = cfg.roundcube_dbtty
13 @db_name = cfg.roundcube_dbname
14 @db_user = cfg.roundcube_dbuser
15 @db_pass = cfg.roundcube_dbpass
16 end
17
18
19 def describe_domain(domain)
20 # Roundcube doesn't have a concept of domains.
21 return domain
22 end
23
24 def describe_user(user)
25 user_id = self.get_user_id(user)
26
27 if user_id.nil?
28 return 'User not found'
29 else
30 return "User ID: #{user_id}"
31 end
32 end
33
34
35 def list_users()
36 # Produce a list of Roundcube users. This is used in prune/rm, and
37 # is public because it is useful in testing.
38 usernames = []
39
40 # Just assume PostgreSQL for now.
41 begin
42 connection = PGconn.connect(@db_host,
43 @db_port,
44 @db_opts,
45 @db_tty,
46 @db_name,
47 @db_user,
48 @db_pass)
49
50 sql_query = "SELECT username FROM users;"
51 connection.query(sql_query) do |result|
52 usernames = result.field_values('username')
53 end
54
55 connection.close()
56 rescue PGError => e
57 # Pretend like we're database-agnostic in case we ever are.
58 raise DatabaseError.new(e)
59 end
60
61 return usernames
62 end
63
64 protected;
65
66 def get_user_id(user)
67 user_id = nil
68
69 begin
70 connection = PGconn.connect(@db_host,
71 @db_port,
72 @db_opts,
73 @db_tty,
74 @db_name,
75 @db_user,
76 @db_pass)
77
78 sql_query = "SELECT user_id FROM users WHERE username = $1;"
79
80 connection.query(sql_query, [user]) do |result|
81 if result.num_tuples > 0
82 user_id = result[0]['user_id']
83 end
84 end
85
86 connection.close()
87
88 rescue PGError => e
89 # Pretend like we're database-agnostic in case we ever are.
90 raise DatabaseError.new(e)
91 end
92
93 return user_id
94 end
95
96
97 end