0f80f72dd5ab33bad582fe15298aa4287f7e4ed7
[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 protected;
36
37 def get_user_id(user)
38 user_id = nil
39
40 begin
41 connection = PGconn.connect(@db_host,
42 @db_port,
43 @db_opts,
44 @db_tty,
45 @db_name,
46 @db_user,
47 @db_pass)
48
49 sql_query = "SELECT user_id FROM users WHERE username = $1;"
50
51 connection.query(sql_query, [user]) do |result|
52 if result.num_tuples > 0
53 user_id = result[0]['user_id']
54 end
55 end
56
57 connection.close()
58
59 rescue PGError => e
60 # Pretend like we're database-agnostic in case we ever are.
61 raise DatabaseError.new(e)
62 end
63
64 return user_id
65 end
66
67
68 # Used in both prune/rm.
69 def list_users()
70 usernames = []
71
72 # Just assume PostgreSQL for now.
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 username FROM users;"
83 connection.query(sql_query) do |result|
84 usernames = result.field_values('username')
85 end
86
87 connection.close()
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 usernames
94 end
95
96 end